Can you please help me in understanding the different entity relationship with simple examples. It is realy confusing to find the owner of the relationship in @OneToOne, @OneToMany and @ManyToOne.How to find where to apply @OneToMany and @ManyToOne in bidirectional entity relationship. I am also confused with the use of mappedBy element.
Consider that the owning side drives the relationship upgrade. mappedBy is required in non-owning side of your relationship to say that you want a bidirectional relationship. So, for example, if you have a one-to-one bidiretional relationship between Wife and Husband, and the owning side is Wife, saving the Husband object won't affect its relationship with Wife ( but the cascading rules apply). In this case, Wife object will have a Husband one-to-one relationship with a "mappedBy" attribute. In general, is up to you to decide the owning side of your relationship, where applyable (for example, the one-to-many relationship cannot have a "one" owning side).
Actually, To design any application, the first thing everybody do is to design the Database. In database design, everyone designs the tables and the relationships between them. Now, let say there are two tables called Employee and Address. Now, there is One-to-One relationship exists between them. Now, It is up to you to decide which holds the primary key of the other table. Lets say, Employee table has the reference of the Address table primary key, then the employee table would be the owner of the relationship. Now, We need to design our entities to have the table structure like above. We keep the mapped by element in the entity that is not the owner. so, The mapped by element would go into the Address entity. similarly the case with the rest of the Entity relationships. whatever be the relationship, first design the database and then design your entities. Hope this helps.
Thanks Mirko and Krishna... I understood the example for @oneToone
Consider the below example for @OneToMany and @ManyToOne bidirectional relationship.
is the above example correct?
EJB3 in Action says that mappedBy element should always be present with @OneToMany
posted 12 years ago
No, the "mappedBy" should be present on the "one" side of a many-to-one relationship if it is BIDIRECTIONAL (only the "many" side can be the owner of that kind of relationship). The schema difference between unidirectional and bidirectional is that if you create an unidirectional one-to-many with the "one" side as the owner, you should have a @JoinTable to do that; instead, if it's bidirectional, the owner should be the "many" side and you have the fk columns on the "many" side entity.
Hope this helps.
posted 12 years ago
Let me tell you a tip how to place the mapped by element in case of many-to-one and one-to-many relationship. lets have an example: Manager and employee. A single Manager can have many employees and a single Employee would have only one Manager. In the database, I would design it in such a way that, in every employee row, I would have a Manager ID so that for a given Manager, I would get a list of employees. so, in this case, It would be best to have the ManagerID reference in the employee table. So, the employee should be the owner of the relationship. That is why, ManyToOne annotation would not have mapped by element as onetomany can not be the owner. Now let come to entities. We need to design them according to the database.
First understand the database. draw the tables and fill them with few examples. Then design the entities. Hope you understand.