• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is inverse in hibernate

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

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


Suggestions highly appreciated ....


Thanks
GRK
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ramakrishna Gutha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ramakrishna Gutha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


For Hibernate inverse :

http://www.javakeexample.com/2013/01/inverse-true-example-and-explanation.html
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic