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
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.
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
Joined: Jan 18, 2010
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
ADDRESS[ id, person_id, city, street]
So can i say the company is inverse side (inverse=true).
Please validate it.
Joined: Sep 08, 2006
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".
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),
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.