-->
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.  [ 1 post ] 
Author Message
 Post subject: Problem with composite id delete
PostPosted: Tue Jun 06, 2006 3:36 am 
Newbie

Joined: Mon Feb 07, 2005 3:12 am
Posts: 2
Hi,
I have problem trying to delete off a composite child object. Below are my code

Hibernate version: 2.1.7

Mapping documents:

CharacterInventory.hbm.xml

<hibernate-mapping>

<class name="mod.user.CharacterInventory" table="character_inventory" optimistic-lock="version" dynamic-update="true" dynamic-insert="true">

<composite-id>
<key-many-to-one name="gameCharacter" class="mod.user.GameCharacter" column="char_id" />
<key-many-to-one name="item" class="mod.item.Item" column="item_id" />
</composite-id>

<version column="version" name="version" type="java.lang.Long" unsaved-value="negative"/>

<property name="quantity">
<column name="quantity" sql-type="int(11)" not-null="false"/>
</property>

<property name="equip">
<column name="equip" sql-type="int(11)" not-null="false"/>
</property>
</class>


</hibernate-mapping>

GameCharacter.hbm.xml

<hibernate-mapping>

<class name="mod.user.GameCharacter" table="game_character">

<id name="charId" type="long" unsaved-value="null" >
<column name="char_id" sql-type="Int(11)" not-null="true"/>
<generator class="identity"/>
</id>

<property name="nickName">
<column name="nick_name" sql-type="char(10)" not-null="true"/>
</property>

<set name="CharacterInventory" table="character_inventory" inverse="false" cascade="all" order-by="item_id asc" lazy="true">
<key column="char_id" />
<one-to-many class="mod.user.CharacterInventory" />
</set>
</class>

</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():
try{
GameCharacter p = (GameCharacter) sess.find(query,id,Hibernate.STRING).get(0);
// only for primary key
Transaction t = sess.beginTransaction();

for(int i=0;i<ids.length;i++)
{
itemId=(String)ids[i];
qty=0;
equips=0;
if(quantity.length > i)
{
qty=new Long(quantity[i]).longValue();
equips=new Long(equip[i]).longValue();
}

Item item=(Item) sess.find(query1,itemId,Hibernate.STRING).get(0);
Iterator iter = p.getCharacterInventory().iterator();
found=false;

while (iter.hasNext()) {
CharacterInventory ci = (CharacterInventory) iter.next();
if(ci.getItem().equals(item))
{
if(qty <= 0)
{


toDelete.add(ci);
}
else
{
ci.setQuantity(qty);
ci.setEquip(equips);
}
}
} // end while
}// end for


Iterator iter2 = toDelete.iterator();
while(iter2.hasNext()){
CharacterInventory ci = (CharacterInventory) iter2.next();
// This is the remove potion
p.getCharacterInventory().remove(ci);
}

t.commit();
}catch(Exception e){
System.out.println(e.toString());
}




After the deletion, it does work. The object is remove from the gameCharacter. However when I check the database, the record remain in character_Inventory table , only that the char_id in that record become 0. Thus gameCharacter don retrieve this record anymore. How should I do the mapping such that when I remove this characterInventory from the gameCharacter I can totally remove it from the character_inventory table? I have try to use cascade="all-delete-orphan" before but it does the same thing when I deleted the item. Can anyone point out to me which part I gone wrong? Thanks.


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

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.