Sydney
Sydney

Reputation: 12212

What does relationship owner means in bidirectional relationship?

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

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?

Upvotes: 39

Views: 24794

Answers (3)

Bisrat
Bisrat

Reputation: 21

Bidirectional relationships must follow these rules:

  1. The inverse side must refer to its owning sideby using the mappedBy attribute
  2. The owning side may have the inversedBy attribute
  3. @ManyToOne is always the owning side of a bidirectional association
  4. @OneToMany is always the inverse side of a bidirectional association
  5. the owning side of a @OneToOne association is the entity with the table containing the foreign key
  6. The many side of many-to-one bidirectional relationships must not define the mappedBy attribute
  7. For many-to-many bidirectional relationships, either side may be the owning side

Upvotes: 0

Dammike
Dammike

Reputation: 21

It is also important to remember that without mappedBy you will run into an issue of circular dependencies. This happens when there is no owner in a bidirectional relationship.

In a circular dependency you will (explicitly) set both sides of the entities before saving

thisObj.set(thatObj);
thatObj.set(thisObj);

Upvotes: 0

Mikko Maunu
Mikko Maunu

Reputation: 42074

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).

Upvotes: 27

Related Questions