![]() | |
|
Bidirectional OneToOne Relationships
Assuming that:
Entity A references a single instance of Entity B.
Entity B references a single instance of Entity A.
Entity A is specified as the owner of the relationship.
The following mapping defaults apply:
Entity A is mapped to a table named A.
Entity B is mapped to a table named B.
Table A contains a foreign key to table B. The foreign key column name is formed as the concatenation of the following: the name of the relationship property or field of entity A; "_"; the name of the primary key column in table B. The foreign key column has the same type as the primary key of table B and there is a unique key constraint on it.
Example:
@Entity public class Employee { private Cubicle assignedCubicle; @OneToOne public Cubicle getAssignedCubicle() { return assignedCubicle; } public void setAssignedCubicle(Cubicle cubicle) { this.assignedCubicle = cubicle; } ... }
@Entity public class Cubicle { private Employee residentEmployee; @OneToOne(mappedBy="assignedCubicle") public Employee getResidentEmployee() { return residentEmployee; } public void setResidentEmployee(Employee employee) { this.residentEmployee = employee; } ... }
In this example:
Entity Employee references a single instance of Entity Cubicle.
Entity Cubicle references a single instance of Entity Employee.
Entity Employee is the owner of the relationship.
The following mapping defaults apply:
Entity Employee is mapped to a table named EMPLOYEE.
Entity Cubicle is mapped to a table named CUBICLE.
Table EMPLOYEE contains a foreign key to table CUBICLE. The foreign key column is named ASSIGNEDCUBICLE_<PK of CUBICLE>, where <PK of CUBICLE> denotes the name of the primary key column of table CUBICLE. The foreign key column has the same type as the primary key of CUBICLE, and there is a unique key constraint on it.
Bidirectional ManyToOne / OneToMany Relationships
Assuming that:
Entity A references a single instance of Entity B.
Entity B references a collection of Entity A.
Entity A must be the owner of the relationship.
The following mapping defaults apply:
Entity A is mapped to a table named A.
Entity B is mapped to a table named B.
Table A contains a foreign key to table B. The foreign key column name is formed as the concatenation of the following: the name of the relationship property or field of entity A; "_"; the name of the primary key column in table B. The foreign key column has the same type as the primary key of table B.
Example:
@Entity public class Employee { private Department department; @ManyToOne public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } ... }
@Entity public class Department { private Collection<Employee> employees = new HashSet(); @OneToMany(mappedBy="department") public Collection<Employee> getEmployees() { return employees; } public void setEmployees(Collection<Employee> employees) { this.employees = employees; } ... }
In this example:
Entity Employee references a single instance of Entity Department.
Entity Department references a collection of Entity Employee.
Entity Employee is the owner of the relationship.
The following mapping defaults apply:
Entity Employee is mapped to a table named EMPLOYEE.
Entity Department is mapped to a table named DEPARTMENT.
Table EMPLOYEE contains a foreign key to table DEPARTMENT. The foreign key column is named DEPARTMENT_<PK of DEPARTMENT>, where <PK of DEPARTMENT> denotes the name of the primary key column of table DEPARTMENT. The foreign key column has the same type as the primary key of DEPARTMENT.
Bidirectional ManyToMany Relationships
Assuming that:
Entity A references a collection of Entity B.
Entity B references a collection of Entity A.
Entity A is the owner of the relationship.
The following mapping defaults apply:
Entity A is mapped to a table named A.
Entity B is mapped to a table named B.
There is a join table that is named A_B (owner name first). This join table has two foreign key columns. One foreign key column refers to table A and has the same type as the primary key of table A. The name of this foreign key column is formed as the concatenation of the following: the name of the relationship property or field of entity B; "_"; the name of the primary key column in table A. The other foreign key column refers to table B and has the same type as the primary key of table B. The name of this foreign key column is formed as the concatenation of the following: the name of the relationship property or field of entity A; "_"; the name of the primary key column in table B.
Example:
@Entity public class Project { private Collection<Employee> employees; @ManyToMany public Collection<Employee> getEmployees() { return employees; } public void setEmployees(Collection<Employee> employees) { this.employees = employees; } ... }
@Entity public class Employee { private Collection<Project> projects; @ManyToMany(mappedBy="employees") public Collection<Project> getProjects() { return projects; } public void setProjects(Collection<Project> projects) { this.projects = projects; } ... }
In this example:
Entity Project references a collection of Entity Employee.
Entity Employee references a collection of Entity Project.
Entity Project is the owner of the relationship.
The following mapping defaults apply:
Entity Project is mapped to a table named PROJECT.
Entity Employee is mapped to a table named EMPLOYEE.
There is a join table that is named PROJECT_EMPLOYEE (owner name first). This join table has two foreign key columns. One foreign key column refers to table PROJECT and has the same type as the primary key of PROJECT. The name of this foreign key column is PROJECTS_<PK of PROJECT>, where <PK of PROJECT> denotes the name of the primary key column of table PROJECT. The other foreign key column refers to table EMPLOYEE and has the same type as the primary key of EMPLOYEE. The name of this foreign key column is EMPLOYEES_<PK of EMPLOYEE>, where <PK of EMPLOYEE> denotes the name of the primary key column of table EMPLOYEE.
Unidirectional OneToOne Relationships
Assuming that:
Entity A references a single instance of Entity B.
Entity B does not reference Entity A.
A unidirectional relationship has only an owning side, which in this case must be Entity A.
The following mapping defaults apply:
Entity A is mapped to a table named A.
Entity B is mapped to a table named B.
Table A contains a foreign key to table B. The foreign key column name is formed as the concatenation of the following: the name of the relationship property or field of entity A; "_"; the name of the primary key column in table B. The foreign key column has the same type as the primary key of table B and there is a unique key constraint on it.
Example:
@Entity public class Employee { private TravelProfile profile; @OneToOne public TravelProfile getProfile() { return profile; } public void setProfile(TravelProfile profile) { this.profile = profile; } ... }
@Entity public class TravelProfile { ... }
In this example:
Entity Employee references a single instance of Entity TravelProfile.
Entity TravelProfile does not reference Entity Employee.
Entity Employee is the owner of the relationship.
The following mapping defaults apply:
Entity Employee is mapped to a table named EMPLOYEE.
Entity TravelProfile is mapped to a table named TRAVELPROFILE.
Table EMPLOYEE contains a foreign key to table TRAVELPROFILE. The foreign key column is named PROFILE_<PK of TRAVELPROFILE>, where <PK of TRAVELPROFILE> denotes the name of the primary key column of table TRAVELPROFILE. The foreign key column has the same type as the primary key of TRAVELPROFILE, and there is a unique key constraint on it.
Unidirectional ManyToOne Relationships
Assuming that:
Entity A references a single instance of Entity B.
Entity B does not reference Entity A.
A unidirectional relationship has only an owning side, which in this case must be Entity A.
The following mapping defaults apply:
Entity A is mapped to a table named A.
Entity B is mapped to a table named B.
Table A contains a foreign key to table B. The foreign key column name is formed as the concatenation of the following: the name of the relationship property or field of entity A; "_"; the name of the primary key column in table B. The foreign key column has the same type as the primary key of table B.
Example:
@Entity public class Employee { private Address address; @ManyToOne public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } ... }
@Entity public class Address { ... }
In this example:
Entity Employee references a single instance of Entity Address.
Entity Address does NOT reference Entity Employee.
Entity Employee is the owner of the relationship.
The following mapping defaults apply:
Entity Employee is mapped to a table named EMPLOYEE.
Entity Address is mapped to a table named ADDRESS.
Table EMPLOYEE contains a foreign key to table ADDRESS. The foreign key column is named ADDRESS_<PK of ADDRESS>, where <PK of ADDRESS> denotes the name of the primary key column of table ADDRESS. The foreign key column has the same type as the primary key of ADDRESS.
Unidirectional OneToMany Relationships
Assuming that:
Entity A references a collection of Entity B.
Entity B does not reference Entity A.
A UNIDIRECTIONAL relationship has only an owning side, which in this case must be Entity A.
The following mapping defaults apply:
Entity A is mapped to a table named A.
Entity B is mapped to a table named B.
There is a join table that is named A_B (owner name first). This join table has two foreign key columns. One foreign key column refers to table A and has the same type as the primary key of table A. The name of this foreign key column is formed as the concatenation of the following: the name of entity A; "_"; the name of the primary key column in table A. The other foreign key column refers to table B and has the same type as the primary key of table B and there is a unique key constraint on it. The name of this foreign key column is formed as the concatenation of the following: the name of the relationship property or field of entity A; "_"; the name of the primary key column in table B.
Example:
@Entity public class Employee { private Collection<AnnualReview> annualReviews; @OneToMany public Collection<AnnualReview> getAnnualReviews() { return annualReviews; } public void setAnnualReviews(Collection<AnnualReview> annualReviews) { this.annualReviews = annualReviews; } ... }
@Entity public class AnnualReview { ... }
In this example:
Entity Employee references a collection of Entity AnnualReview.
Entity AnnualReview does NOT reference Entity Employee.
Entity Employee is the owner of the relationship.
The following mapping defaults apply:
Entity Employee is mapped to a table named EMPLOYEE.
Entity AnnualReview is mapped to a table named ANNUALREVIEW.
There is a join table that is named EMPLOYEE_ANNUALREVIEW (owner name first). This join table has two foreign key columns. One foreign key column refers to table EMPLOYEE and has the same type as the primary key of EMPLOYEE. This foreign key column is named EMPLOYEE_<PK of EMPLOYEE>, where <PK of EMPLOYEE> denotes the name of the primary key column of table EMPLOYEE. The other foreign key column refers to table ANNUALREVIEW and has the same type as the primary key of ANNUALREVIEW. This foreign key column is named ANNUALREVIEWS_<PK of ANNUALREVIEW>, where <PK of ANNUALREVIEW> denotes the name of the primary key column of table ANNUALREVIEW. There is a unique key constraint on the foreign key that refers to table ANNUALREVIEW.
Unidirectional ManyToMany Relationships
Assuming that:
Entity A references a collection of Entity B.
Entity B does not reference Entity A.
A UNIDIRECTIONAL relationship has only an owning side, which in this case must be Entity A.
The following mapping defaults apply:
Entity A is mapped to a table named A.
Entity B is mapped to a table named B.
There is a join table that is named A_B (owner name first). This join table has two foreign key columns. One foreign key column refers to table A and has the same type as the primary key of table A. The name of this foreign key column is formed as the concatenation of the following: the name of entity A; "_"; the name of the primary key column in table A. The other foreign key column refers to table B and has the same type as the primary key of table B. The name of this foreign key column is formed as the concatenation of the following: the name of the relationship property or field of entity A; "_"; the name of the primary key column in table B.
Example:
@Entity public class Employee { private Collection<Patent> patents; @ManyToMany public Collection<Patent> getPatents() { return patents; } public void setPatents(Collection<Patent> patents) { this.patents = patents; } ... }
@Entity public class Patent { ... }
In this example:
Entity Employee references a collection of Entity Patent.
Entity Patent does NOT reference Entity Employee.
Entity Employee is the owner of the relationship.
The following mapping defaults apply:
Entity Employee is mapped to a table named EMPLOYEE.
Entity Patent is mapped to a table named PATENT.
There is a join table that is named EMPLOYEE_PATENT (owner name first). This join table has two foreign key columns. One foreign key column refers to table EMPLOYEE and has the same type as the primary key of EMPLOYEE. This foreign key column is named EMPLOYEE_<PK of EMPLOYEE>, where <PK of EMPLOYEE> denotes the name of the primary key column of table EMPLOYEE. The other foreign key column refers to table PATENT and has the same type as the primary key of PATENT. This foreign key column is named PATENTS_<PK of PATENT>, where <PK of PATENT> denotes the name of the primary key column of table PATENT.
![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |