• 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

One-to-Many Bidirectional Inverse Issue

Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose i have one-to-many bidirectional association between Category and Item
In Category.hbm.xml:
<class name="Category" table="CATEGORY">
<set name="items" cascade="save-update">
<column name="CAT_ID" not-null="true" />
<one-to-many class="Item" />


In Item.hbm.xml:
<many-to-one name="category" class="Category" update="false" insert="false" column name="CAT_ID" />

Suppose i have a category with 2 items and now i am saving category object.
insert into HBLEARN1.CATEGORY (CNAME, CID) values ('Computer', 100)
insert into HBLEARN1.ITEM (INAME, ITEMID, CAT_ID) values ('Pen', 1, 100)
insert into HBLEARN1.ITEM (INAME, ITEMID, CAT_ID) values ('Pencil', 2, 100)
update HBLEARN1.ITEM set CAT_ID=100 where ITEMID=1 and CAT_ID=100
update HBLEARN1.ITEM set CAT_ID=100 where ITEMID=2 and CAT_ID=100

NOTE1 : I have not specified inverse="true" attribute on items collection in mapping.
NOTE2 : The many-to-one side is mapped with update="false" and insert="false"

Now there are 2 changes to inmemoery persistence instances. but as i have marked many-to-one side as update="false"
and insert="false", it will not participate in any SQL. Moreover as there is no inverse="true" specified at one-to-many side,
it will participate in insert/update SQL's. here i am saving category and items. So i understand the insert statements.

Problem A)why it is firing 2 item update statements?
FYI: if i specify inverse="true" on one-to-many side, these 2 update statements are not fired..

Problem B)In Java persistence with hibernate book its written that, i can switch the inverse side. The <many-to-one> element
doesn’t have an inverse attribute, but we can map it with update="false" and insert="false" to effectively ignore it
for any UPDATE or INSERT statements. If i do this (no inverse="true" on one-to-many side and insert="false" and
update="false" on many-to-one side), still i get those 2 update statements?
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    Bookmark Topic Watch Topic
  • New Topic