-->
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: Collection mapping using <map>: inserts not working
PostPosted: Tue May 04, 2004 8:33 pm 
Newbie

Joined: Mon Mar 01, 2004 9:49 pm
Posts: 13
Hi,
I am having the following problem: I am loading tree structure from a table (item_group) into ItemGroup object.

[color=green]Class ItemGroup implements Serializable{[/color]
private Integer itemId;
private Integer parentItemId;
private Integer topParentItemId;
//map of ItemGroupRelation objects representing children of this ItemGroup.
Map childreMap;
//getters and setters
[color=green]}[/color]

[color=green]Class ItemGroupRelation implements Serializable{[/color]
private Integer groupId;
private Integer itemId;
private Integer parentItemId;
private Integer topParentItemId;
[color=green]}[/color]

ItemGroupRelation object represents a row in item_group table and ItemGroup object is having an extra map of ItemGroupRelation objects representing children of a particulat item.

[color=red]Case1: INSERT DOESN'T WORK, UPDATE WORKS[/color]
If I add a new ItemGroupRelation object to the map of ItemGroup object, it updates that record if its there is the item_group table. If its not there gives an error: (but I want it to insert a row if its not there)

- Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:688)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:641)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2382)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2336)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
at com.vizional.vizidapps.assetmgmt.dao.hibernate.ItemGroupDao.updateItemGroup(ItemGroupDao.java:146)
at com.vizional.vizidapps.assetmgmt.dao.hibernate.ItemGroupDao.main(ItemGroupDao.java:167)

MY table looks like this:
desc item_group;
child_item_id, parent_item_id, top_parent_item_id (child_item_id is UNIQUE).

My mapping looks like this:
<hibernate-mapping>
<class name="ItemGroup" table="item_group">
<id name="itemId" column="child_item_id">
<generator class="assigned"/>
</id>
<property name="parentItemId" column="parent_item_id"/>
<property name="topParentItemId" column="top_parent_item_id"/>
<map name="childGroupRelationMap" table="item_group" cascade="all-delete-orphan">
<key column="parent_item_id"/>
<index column="child_item_id" type="integer"/>
<one-to-many class="itemGroup"/>
</map>
</class>
<class name="ItemGroupRelation" table="item_group">
<id name="childItemId" column="child_item_id">
<generator class="assigned"/>
</id>
<property name="parentItemId" column="parent_item_id"/>
<property name="topParentItemId" column="top_parent_item_id"/>
</class>
</hibernate-mapping>

[color=red]Case2: INSERT WORK, UPDATE DOESN'T WORK[/color]
If I add a new ItemGroupRelation object to the map of ItemGroup object, to change the parent in ItemGroupRelation object, its trying to insert a new record instead of updating the parent of existing record. NO error, but duplicate row with the same childItemId (different parentItemIds).


MY table looks like this:
desc item_group;
group_id, child_item_id, parent_item_id, top_parent_item_id (group_id is PRIMARY, child_item_id is UNIQUE).

My mapping looks like this:
<hibernate-mapping>
<class name="ItemGroup" table="item_group">
<id name="itemId" column="child_item_id">
<generator class="assigned"/>
</id>
<property name="parentItemId" column="parent_item_id"/>
<property name="topParentItemId" column="top_parent_item_id"/>
<map name="childGroupRelationMap" table="item_group" cascade="all-delete-orphan">
<key column="parent_item_id"/>
<index column="child_item_id" type="integer"/>
<one-to-many class="itemGroup"/>
</map>
</class>
<class name="ItemGroupRelation" table="item_group">
<id name="groupRelationId" column="item_group_id">
<generator class="native"/>
</id>
<property name="childItemId" column="child_item_id"/>
<property name="parentItemId" column="parent_item_id"/>
<property name="topParentItemId" column="top_parent_item_id"/>
</class>
</hibernate-mapping>

Suggestions on how I should map so that both Inserts and Updates work..

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.