Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does relationship owner means in bidirectional relationship?

Tags:

hibernate

I have a simple model with a Question and Choice object.

  • ONE Question has MANY CHOICE(S).
  • MANY Choice has ONE Question

There are two ways to implement that with Hibernate

Implementation One: The owner side is Choice

Question.java

@OneToMany (mappedBy="question") private Set choices = new HashSet(); 

Choice.java

@ManyToOne @JoinColumn (name="QUESTION_ID") private Question question; 

Implementation Two: The owner side is Question

Question.java

@OneToMany @JoinColumn (name = "QUESTION_ID") private Set choices = new HashSet(); 

Choice.java

@ManyToOne @JoinColumn (name="QUESTION_ID", updatable = false, insertable = false) private Question question; 

What is the difference between the two implementation?

like image 328
Sydney Avatar asked Jan 19 '12 18:01

Sydney


People also ask

Who is the owner of a relationship in a bidirectional relationship?

The many side is always the owning side of the relationship. For one-to-one bidirectional relationships, the owning side corresponds to the side that contains the corresponding foreign key. For many-to-many bidirectional relationships, either side may be the owning side.

Who is the owner of relationship?

What are Relationship Owners? Relationship Owners allow you to assign your teammates to a journalist to help track and manage your team's most important relationships. The teammates who have been assigned on a journalist profile page are Relationship Owners.

Which is an example of a bidirectional relationship?

Physical and mental health have a bidirectional relationship that can be influenced by many factors, such as exercise and hormones. An example of the bidirectional relationship between mental and physical health can be seen in the association between depression and physical exercise.

How do you make a one to many relationship as bidirectional?

Bidirectional @OneToMany Relationship – Employer/EmployeeWhen you traverse from the “Many” side to the “One” side, you only need to make reference to one object, which is why the Employee class holds a single reference to an Employer class via the private Employer employer instance variable.


1 Answers

Your first example is normal and correct bidirectional one-to-many/many-to-one mapping. Setting Question to Choice-attribute ("owning side") is enough to have relationship persisted. Entity graph in memory will be messed until other side of the relationship is read from database again. From the database point-of-view owner is the entity that is persisted to table that have foreign key column (same for bidirectional one-to-one). In specification this is explained following way:

The many side of one-to-many / many-to-one bidirectional relationships must be the owning side, hence the mappedBy element cannot be specified on the ManyToOne annotation.
....
Bidirectional relationships between managed entities will be persisted based on references held by the owning side of the relationship. It is the developer’s responsibility to keep the in-memory references held on the owning side and those held on the inverse side consistent with each other when they change. In the case of unidirectional one-to-one and one-to-many relationships, it is the developer’s responsibility to insure that the semantics of the relationships are adhered to.

In JPA terms your second example does not have owning side, because of absence of mappedBy. Instead you have two unidirectional relationships which are forced to use same column as store. At least with Hibernate 3.5.6 it will behave following way:

  • Setting Question to choice-attribute will not persist relationship.
  • Adding Choice to question-attribute will not persist relationship.
  • To persist value to "QUESTION_ID" both have to be set (yes, also not-insertable question).
like image 181
Mikko Maunu Avatar answered Sep 21 '22 15:09

Mikko Maunu