When each entity points to the other, the relationship is bidirectional.
If only one entity points to the other, the relationship is unidirectional.
In your particular case, the sentence:
An entity cannot have a unidirectional relationship to the embeddable class of another entity (or itself)
can be translated to:
EntityABCD entity cannot have a unidirectional relationship to the embeddable class EmploymentPeriod of another Employee entity.
so in other words:
If Employee entity has an embedded EmploymentPeriod, it is not possible to define an unidirectional relationship from EntityABCD entity to embeddable EmploymentPeriod of entity Employee.
Why?
Because an embeddable object has no identity of its own (lack of primary key) it needs to be treated only as a part of the entity that encapsulates it. From a database point of view, an embedded object is stored with the rest of the entity attributes in a single row.
As a consequence of the above, if someone would try to create an unidirectional relationship from EntityABCD
the EmploymentPeriod
it is simply not possible due to lack of identity of the EmploymentPeriod
,
thus it is not possible to create the foreign key in the embedded object.
How to overcome the problem with the foreign key?
Foreign key needs to be physically created outside embeddedable class and it depends on type of relationship. An example (:
@Entity
public class Employee {
@Id
private long id;
@Embedded
private EmploymentPeriod period;
}
@Embeddable
public class EmploymentPeriod {
@ManyToOne //owning relationship
@JoinColumn //FK in EMPLOYEE table (by default: ENTITYABCD_ID)
private EntityABCD entityABCD;
@ManyToMany //owning relationship
@MapKey(name="id") //refers to EntityABCD.id
@JoinTable //FK in the join table (by default: EMPLOYEE_ENTITYABCD)
private Map<Long, EntityABCD> entitiesABCD;
}
@Entity
public class EntityABCD {
@Id
private long id;
@OneToMany(mappedBy = "period.entityABCD") //non-owning relationship
List<Employee> employee;
@ManyToMany(mappedBy="period.entitiesABCD") //non-owning relationship
private List<Employee> employees;
}
When bidirectional relationships exist within an embedded object, they are treated as though they exist in the owning entity (Employee
)
and the target entity (EntityABCD
) points back to the owning entity, not to the embedded object (EmploymentPeriod
).
It is worth to mention that embeddables can only embed:
- other embeddables
- relationships to entities
- element collections of basic / embeddable type