aspose file tools*
The moose likes Object Relational Mapping and the fly likes oneToOne bidirectional question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "oneToOne bidirectional question" Watch "oneToOne bidirectional question" New topic
Author

oneToOne bidirectional question

christophe lassere
Greenhorn

Joined: Apr 26, 2011
Posts: 7

hi ,
i have a problem concept with JPA concepts;
for i have a OnetoOne bidirectional relationship between Person and Address

so i have public class Person with address attribute and class Address with person attribute.(with mappedBy in Address side)

in main it works fine if i execute code one line per table :

Person p1 =new Person();
Address a1= new Address();
p1.setAddress(a1); //line 1
em.persist(p1); //line 2

but it doesn't work if i put a1.setPerson(p1); in place of line 1
em.persist(a1); in place of line 2

only one line in adresse

i though it was bidirectionnal definition !!!
can someone help me

Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
Hi Christophe,

Literally that's what bidirectional means but when you talk in terms of save/update/delete operations from JPA's perspective, a bidirectional association has to be owned by one of the two entities involved.

As per your mapping configuration, the Person class owns it that means.....if you try to save a Person object the address object would be saved as well or if you try to delete the Person object, associated address object would also be deleted (provided the cascading attributes are set properly).

If you talk from the select query perspective than yes, it is really a bidirectional concept that means you can access Address if you fetch Person object OR you can access Person if you have Address object.


Regards,
Naresh Waswani


Naresh Waswani
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
You have to update both sides of a bi-directional relationship.

See,
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Object_corruption.2C_one_side_of_the_relationship_is_not_updated_after_updating_the_other_side

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
christophe lassere
Greenhorn

Joined: Apr 26, 2011
Posts: 7

thank you ,

i thought that concept of owning a relationship did not exist in a bidirectionnal relation.

what i understand too is that JPA introduces a limitation in this case because you don t need to implement line 1 in adresse.java if you dont use JPA !!!

public void setEmploye(Employe employe) {
this.employe = employe;
employe.setAdresse(this); //line 1
}

christophe lassere
christophe lassere
Greenhorn

Joined: Apr 26, 2011
Posts: 7

thank you ,

i thought that concept of owning a relationship did not exist in a bidirectionnal relation.

what i understand too is that JPA introduces a limitation in this case because you don t need to implement line 1 in adresse.java if you dont use JPA !!!

public void setEmploye(Employe employe) {
this.employe = employe;
employe.setAdresse(this); //line 1
}

christophe lassere
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: oneToOne bidirectional question