Implement association relationships using persistence entities, including the following associations: bidirectional for @OneToOne, @ManyToOne, @OneToMany, and @ManyToMany; unidirectional for @OneToOne, @ManyToOne, @OneToMany, and @ManyToMany.

[Note]
  • [EJB_3.0_PERSISTENCE] 2.1.8.1; 2.1.8.2; 2.1.8.4; 2.1.8.3; 2.1.8.3.1; 2.1.8.3.2; 2.1.8.5; 2.1.8.5.1; 2.1.8.5.2

Bidirectional OneToOne Relationships

Assuming that:

The following mapping defaults apply:

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:

The following mapping defaults apply:

Bidirectional OneToOne Relationships

Bidirectional ManyToOne / OneToMany Relationships

Assuming that:

Entity A must be the owner of the relationship.

The following mapping defaults apply:

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:

The following mapping defaults apply:

Bidirectional ManyToOne / OneToMany Relationships

Bidirectional ManyToMany Relationships

Assuming that:

The following mapping defaults apply:

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:

The following mapping defaults apply:

Bidirectional ManyToMany Relationships

Unidirectional OneToOne Relationships

Assuming that:

A unidirectional relationship has only an owning side, which in this case must be Entity A.

The following mapping defaults apply:

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:

The following mapping defaults apply:

Unidirectional OneToOne Relationships

Unidirectional ManyToOne Relationships

Assuming that:

A unidirectional relationship has only an owning side, which in this case must be Entity A.

The following mapping defaults apply:

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:

The following mapping defaults apply:

Unidirectional ManyToOne Relationships

Unidirectional OneToMany Relationships

Assuming that:

A UNIDIRECTIONAL relationship has only an owning side, which in this case must be Entity A.

The following mapping defaults apply:

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:

The following mapping defaults apply:

Unidirectional OneToMany Relationships

Unidirectional ManyToMany Relationships

Assuming that:

A UNIDIRECTIONAL relationship has only an owning side, which in this case must be Entity A.

The following mapping defaults apply:

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:

The following mapping defaults apply:

Unidirectional ManyToMany Relationships

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine