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
|