aspose file tools*
The moose likes Object Relational Mapping and the fly likes What is inverse in hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "What is inverse in hibernate" Watch "What is inverse in hibernate" New topic
Author

What is inverse in hibernate

Ramakrishna Gutha
Greenhorn

Joined: Jan 18, 2010
Posts: 16
Hello,

Could anyone explain me what is the need of inverse="true/false" in hibernate.


Suggestions highly appreciated ....


Thanks
GRK
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

This is for bi-directional associations. In the database world, there is no such thing as a bi-directional. You can have a FK field in one table referring to another table.

So if you have a bi-directional assocation in Java like

Order {
List<OrderItems> items
}


OrderItem {
Order order
}

Hibernate won't know that your mapping both sides represents the single FK relationship in the database. So by putting in inverse=true, it tells Hibernate that both mappings map to the same single FK relationship. Otherwise, Hibernate will try to update both sides seperately, and not in any order. So it might try to insert a child before the parent is inserted, and the database will throw an error.

Hope that helps clear it up for you

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ramakrishna Gutha
Greenhorn

Joined: Jan 18, 2010
Posts: 16
Hello Mark,

Thanks for your reply.

So it means that inverse is for the internal purpose of hibernate.

I got one more question, is there any performance gain using inverse attribute in hibernate


Thanks
GRK
Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108
Good explanation by Mark. Yes there is definitely optimization in terms of having inverse ="true" as you tell hibernate which side of the association to fire the sql queries. In case of many to many bi directional you need to make any one end responsible for firing sql queries by making one end with inverse="true" else you would end up with foreign key violation exception.

In case of one-to -many bidirectional association the many-to-one end does not have inverse attribute and is always false. The other end of the association i.e, parent end has to declare inverse="true" and by making that hibernate will not issue sql statements when parent is modified. SQL statements will be issued to the DB only when the association is modified at the child end. So there is a obvious performance optimization.

Hope this solves your query.


Thanks and Regards, Pradeep Kumar
SCJP 1.6, SCWCD 5.0
Ramakrishna Gutha
Greenhorn

Joined: Jan 18, 2010
Posts: 16
Hello Pradeep,

Thanks for the great explanation.

Please validate my statement.

I have a class Company which has multiple Address.



and database relation between company and addresses

COMPANY[id, name]
ADDRESS[ id, person_id, city, street]

So can i say the company is inverse side (inverse=true).

Please validate it.

Thanks
GRK
Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108
Yes, very much true. In case you have declared the address as Set the Address class will be the owner of the association. Hence The other side of the relation i.e Person will have inverse="true".
Ramesh Sengani
Ranch Hand

Joined: Nov 23, 2011
Posts: 36


For Hibernate inverse :

http://www.javakeexample.com/2013/01/inverse-true-example-and-explanation.html
ramesh vanka
Greenhorn

Joined: Mar 13, 2005
Posts: 11
Relationship between two tables will happen with foreignkey / joins /jointables.

Hibernate Supports Uni-directional, not bi-direction like ejb 2.1 cmp (Drawback of ejb 2.1),

means

For ex:
we have two tables
1) goods – (Parent )
2) goods_record – (Child, foreign-key - goodsid)
Relationship between goods to goods_record is the one to many.
Relationship between goods_record to goods is the many to one.

Relationship of goods to goods_record Not Equals(!=) Relationship of goods_record to goods

Foreign-key goodsid maintain relationship between goods to goods_record

Same Foreign-key goodsid maintain relationship between goods_record to goods.

Relationship of Goods to Goods_Record:
Here Relationship owner is Goods, this relationshipowner will take care of parent and child relationship with updation of foreign-key, because foreign-key maintain relationship both of them.

Relationship Goods_Record to Goods
Here Relationship owner is Goods_Record, this relationshipowner will take care of parent and child relationship with updation of foreign-key, because foreign-key maintain relationship both of them.


In Bi-direction means we have 2 uni-directions like
1)Goods to Goods_Record
2) Goods_Record to Goods

In this scenario foreign-key will by updated by twice, which is not necessary because one uni-direction is enough maintain relationship between two tables.

To avoid the foreign-key updation twice, hibernate introduces “inverse=true”, it will maintain the relationship means nothing like uni-directional, so that we are avoided updation foreign-key twice.

Hope it will clarify all your doubts.

For source code with explanation, please contact me: ramesh.niwas@gmail.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is inverse in hibernate