Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

oneToOne bidirectional question

 
christophe lassere
Greenhorn
Posts: 7
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
christophe lassere
Greenhorn
Posts: 7
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic