-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Cascade Problems?
PostPosted: Wed Apr 25, 2007 4:00 am 
Newbie

Joined: Wed Apr 25, 2007 3:52 am
Posts: 3
dear guys,

I am using Hibernate 3.0.5, and there's many-to-many relation between Item and Tag object,

there's also one-to-many between Game and Tag.

the hibernate .hbm.xml fragment is as below:

Game.hbm.xml
<set name="tags" lazy="true" cascade="all" order-by="ITEM_INDEX asc">
<key column="GAME_ID"/>
<one-to-many class="com.domain.Tag" />
</set>

Item.hbm.xml
<set name="tags" lazy="true" table="ITEM_TAG">
<key column="ITEM_ID"/>
<many-to-many column="TAG_ID" class="com.domain.Tag" />
</set>


tag.hbm.xml
<property name="tagName" column="TAG_NAME" not-null="true" />
<property name="itemType" column="ITEM_TYPE" not-null="true" />
<property name="itemIndex" column="ITEM_INDEX" type="integer" not-null="false" />
<property name="gameId" column="GAME_ID" type="long" update="false" not-null="true" />

in the program, we add new tag to persistent game first, then add the tag to item.

nomally, it works well, the sql statement is below(we just add one new Item at one time):
[LOGGER] 2007-04-25 09:47:06 DEBUG org.hibernate.SQL | insert into tag (TAG_NAME, ITEM_TYPE, ITEM_INDEX, GAME_ID, TAG_ID) values (?, ?, ?, ?, ?)
[LOGGER] 2007-04-25 09:47:06 DEBUG org.hibernate.SQL | insert into ITEM (STATUS, GSC_ID, GSC_HOLDING_ID, ITEMNAME, ITEMTYPE, ITEMQTY, ITEMPROPERTY, bundle_id, OWNER_ID, SALELISTING_ID, BELONGED_GAME_ACCOUNT, TRANSFER_GAME_TARGET, BRIEFDESCRIPTION, LOCK_DATE, WITHDRAW_DATE, salable_type, ITEM_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'ITEM', ?)
[LOGGER] 2007-04-25 09:47:06 DEBUG org.hibernate.SQL | update tag set GAME_ID=? where TAG_ID=?
[LOGGER] 2007-04-25 09:47:06 DEBUG org.hibernate.SQL | insert into ITEM_TAG (ITEM_ID, TAG_ID) values (?, ?)

sometimes the hibernate will generate the sql below(we add many new Items at one time):

[LOGGER] 2007-04-25 09:47:06 DEBUG org.hibernate.SQL | insert into ITEM (STATUS, GSC_ID, GSC_HOLDING_ID, ITEMNAME, ITEMTYPE, ITEMQTY, ITEMPROPERTY, bundle_id, OWNER_ID, SALELISTING_ID, BELONGED_GAME_ACCOUNT, TRANSFER_GAME_TARGET, BRIEFDESCRIPTION, LOCK_DATE, WITHDRAW_DATE, salable_type, ITEM_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'ITEM', ?)
[LOGGER] 2007-04-25 09:47:06 DEBUG org.hibernate.SQL | insert into tag (TAG_NAME, ITEM_TYPE, ITEM_INDEX, GAME_ID, TAG_ID) values (?, ?, ?, ?, ?)
[LOGGER] 2007-04-25 09:47:06 DEBUG org.hibernate.SQL | insert into tag (TAG_NAME, ITEM_TYPE, ITEM_INDEX, GAME_ID, TAG_ID) values (?, ?, ?, ?, ?)
[LOGGER] 2007-04-25 09:47:06 DEBUG org.hibernate.SQL | update tag set GAME_ID=null where GAME_ID=? and TAG_ID=?

the last sql statement will trigger the database exception,because the constraint on gamd_id,and also this statement is wired.

any suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 5:06 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
HI Battlefly,


update this

Item.hbm.xml

<set name="tags" lazy="true" cascade="all" order-by="ITEM_INDEX asc" inverse="true">
<key column="GAME_ID"/>
<one-to-many class="com.domain.Tag" />
</set>

and in
tag.hbm.xml

<property name="gameId" column="GAME_ID" type="long" update="false" insert="false" not-null="true" />

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 9:02 pm 
Newbie

Joined: Wed Apr 25, 2007 3:52 am
Posts: 3
if I do as you said, then the relation table item_tag is gone, and the many-to-many relation between Item and Tag will disappear.

What I want to know is why Hibernate generate the sql as that, why it will update tag table with null game_id?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 11:42 pm 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi Battlefly,

It is because of inverse mapping. If you set inverse =true then Hibernate will not do like this. If you use Bidirectional mapping with inverse true .It would generate less SQL update.Thses are Synchronization step with session mapped persistent inverse false object.

you might get more idea on

http://simoes.org/docs/hibernate-2.1/155.html under
Second Let's have a look at a relationship update Heading

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 28, 2007 1:59 am 
Newbie

Joined: Wed Apr 25, 2007 3:52 am
Posts: 3
seems that make TAG in GAME inverse can work, thanks for replying.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.