Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

One-to-Many Bidirectional Inverse Issue  RSS feed

Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • 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?
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!