Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Inverse attribute

I am creating a one-to-many relationship. so, i have a parent and a child. The cascade attribute is set to all.

I was wondering, if we consider the following piece of code:

Parent p = (Parent) session.load(Parent.class, pid); 
Child c = new Child("child element");
p.addChild(c);
session.flush();
  • Q1) If the parent owns the relationship, as in , for the parent inverse=false, then would the child element addition be updated in teh database?
  • Q2) If the child owns the relationship, as in , for the parent inverse=true, then will the child element addtion be updated in the databse?
  • Q3) Who owns the relationahsip does not make a difference in the above code in terms of whether the updaet will be seen or not?

thanks a lot

like image 568
TimeToCodeTheRoad Avatar asked Jun 21 '11 10:06

TimeToCodeTheRoad


People also ask

What is inverse attribute in Hibernate?

inverse="true" basically means that the inverse relationship is also mapped within the class definition of the other class. But, it's real meaning is that it defines which side is the parent or the relationship owner for the two entities (parent or child).

Which association use the inverse attribute in Hibernate?

In Hibernate, only the “relationship owner” should maintain the relationship, and the “inverse” keyword is created to defines which side is the owner to maintain the relationship. However the “inverse” keyword itself is not verbose enough, I would suggest change the keyword to “relationship_owner“.

What is the use of inverse true in bidirectional?

The inverse equals true tells NHibernate which side of the relationship to ignore. When you apply it to the collection side and NHibernate will always update the foreign key from the other side, from the child object side.

What is lazy false in Hibernate?

To use lazy collection, you may optionally use lazy="true" attribute in your collection. It is by default true, so you don't need to do this. If you set it to false, all the child objects will be loaded initially which will decrease performance in case of big data.


2 Answers

Case inverse = false:

In this case, it is parent's responsibility to save-update child and its relationship. So in your example, child will be updated in database. There will be two sql queries: 1) Insert child. 2) Update child with foreign key of parent id.

Case Inverse = true:

In this case , it is child's responsibility to save-update itself. So in your code, child will be saved in database but foreign key of parent will be null. Only one sql query will be executed and that is of insert child. For updating parent's foreign key, you need to manually save child.

Child child = new Child();
child.setParent(parent);
session.save(child);

I think, answer of these cases explains answer of your third question.

Hope this help.

like image 188
Nikunj Avatar answered Oct 07 '22 20:10

Nikunj


Inverse is only to tell NH that the foreign key is mapped twice, usually as a one-to-many and a many-to-one, and that it therefore only needs to be stored from one side.

Q1) the child is stored by cascade, but the parent-FK is null. (Except you set the parent relation in the child within p.addChild(c).)

Q2) same as Q1.

Q3) exactly.

like image 42
Stefan Steinegger Avatar answered Oct 07 '22 20:10

Stefan Steinegger