wood burning stoves 2.0*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EJB3 Entity Relationship Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "EJB3 Entity Relationship" Watch "EJB3 Entity Relationship" New topic
Author

EJB3 Entity Relationship

hashmukh punamia
Greenhorn

Joined: Aug 14, 2007
Posts: 22
Hi Ranchers,

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.

Thanks & Regards,
Hashmukh
Mirko Bonasorte
Ranch Hand

Joined: May 14, 2007
Posts: 244
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).

Hope this helps


SCJP<br />SCWCD 1.4 Upgrade (Remember: me stupid)<br />SCWCD 1.4<br /><a href="http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html" target="_blank" rel="nofollow">SCBCD 5.0</a><br /><a href="http://www.enthuware.com" target="_blank" rel="nofollow">SCBCD 5.0 mock exam</a> <br /> <br />SCEA 5 Part1: Preparing...
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
Hi,

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&Regards, Krishna.
SCJP1.4, SCWCD1.4, SCBCD 5.0
hashmukh punamia
Greenhorn

Joined: Aug 14, 2007
Posts: 22
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
Mirko Bonasorte
Ranch Hand

Joined: May 14, 2007
Posts: 244
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.
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
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.
hashmukh punamia
Greenhorn

Joined: Aug 14, 2007
Posts: 22
Thanks Mirko
Thanks Krishna for a very good example..

Once the DB tables are related, then it becomes fairly simple..

Thanks & Regards,
Hashmukh
 
 
subject: EJB3 Entity Relationship