Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

who is owner in association with jpa and hibernate?

I am confused in owner in relationship with hibernate.

what exactly is owner(owner side) in association?

I want to study Mappedby and inverse. please help.

like image 497
user3543851 Avatar asked Nov 30 '22 01:11

user3543851


1 Answers

As a general rule the owning side of a relation would the side which you'd need to update for the change of the relation to be persisted.

If you are mapping the entities to a relational database (most probably the case) the owning side often can be identified as the entity whose table contains the foreign key.

In the entities themselves mappedBy would refer to the owning side and thus is placed on the inverse side of the relation.

In 1:n relations in most cases the owning side is the n side, in n:m relations, 1:1 relations or 1:n with mapping tables you can choose either side, just choose one.

Example:

class Thread {
  @OneToMany( mappedBy = "thread" )
  List<Entry> entries;
}

class Entry {
  @ManyToOne
  Thread thread;
}

In the example, the owning side would be the Entry entity, since you need to change the value of Entry#thread to change the thread an entry belongs to. Just adding/removing an entry to/from Thread#entries wouldn't make the changes persisted in most cases (orphanRemoval and the like would still have an effect if done correctly).

like image 78
Thomas Avatar answered Dec 05 '22 12:12

Thomas