There is an old adage that says every story has three sides: yours, mine, and the truth. Relationships
are kind of the same in that there are three different perspectives. The first is the view from one side of
the relationship, the second is from the other side, and the third is from a global perspective that knows
about both sides. The “sides” are called roles. In every relationship there are two entities that are
related to one another, and each entity is said to play a role in the relationship.
Relationships are everywhere, so examples are not hard to come by. An employee has a
relationship to the department that he or she works in. The Employeeentity plays the role of working in
the department, while the Departmententity plays the role of having an employee working in it.
Of course, the role a given entity is playing differs according to the relationship, and an entity
might be participating in many different relationships with many different entities. We can conclude,
therefore, that any entity might be playing a number of different roles in any given model. If we think
of an Employeeentity, we realize that it does, in fact, play other roles in other relationships, such as the
role of working for a manager in its relationship with another Employeeentity, working on a project in
its relationship with the Projectentity, and so forth.
Unlike EJB 2.1, where the roles all had to be enumerated in metadata for every relationship, JPA
does not have metadata requirements to declare the role an entity is playing. Nevertheless, roles are
still helpful as a means of understanding the nature and structure of relationships.
đang được dịch, vui lòng đợi..