-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problem saving Map with parent object
PostPosted: Wed Jul 28, 2004 3:50 pm 
Newbie

Joined: Wed Jul 28, 2004 3:20 pm
Posts: 16
I've been fighting this one for a few days and I'm completely at a loss. I've read the docs, examples, forums, etc.

I have a parent object with a map of Integer->Integer. The parent is saved initially and then the map is created. The parent is updated. Hibernate sees my collection and DELETES IT! I've tried everything, saving the parent/map together (no update, just 1 save), using saveOrUpdate() (with an interceptor for null-value), cascade=all, cascade=save-update, lazy=true, etc. etc. Nothing seems to work. Any help would be appreciated. I'm really at a loss on this. I have another class that uses a set and that works just fine.

Prereqs:

Hibernate 2.1
Database Oracle 10
I'm using Spring as well!

Mapping document:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping schema="Rick">
<class name="com.stormhq.entity.ProductGroup" table="PRODUCT_GROUP">
<id name="id" column="pg_id">
<generator class="assigned"/>
</id>
<property name="code1" column="code1" length="20"/>
<property name="code2" column="code2" length="20"/>
<property name="displayRank" column="display_rank" length="22"/>
<property name="listPrice" column="list_price" length="126"/>
<property name="longDescription" column="long_description" length="255"/>
<property name="saleString" column="sale_string" length="255"/>
<property name="name" column="name" length="50"/>
<property name="shortDescription" column="short_description" length="50"/>
<property name="visible" column="visible" length="22"/>
<map name="attrGroupMap" table="prod_group_att_group_map" cascade="all">
<key column="pg_id"/>
<index column="ag_id" type="integer"/>
<element column="attr_id" type="integer"/>
</map>
</class>
</hibernate-mapping>


relevant java code:


public class ProductGroup {
Integer id;
String name;
String code1; // there are 2 "backend" codes
String code2;
float listPrice;
String longDescription;
String shortDescription;
String saleString;
int displayRank;
boolean visible;
Map attrGroupMap; // map of AttributeGroup Ids to Attribute ids;
private static Log log = LogFactory.getLog(ProductGroup.class);

public ProductGroup() {
this.attrGroupMap = new HashMap();
}

public Map getAttrGroupMap() {
return attrGroupMap;
}

public void setAttrGroupMap(Map m) {
attrGroupMap.clear();
attrGroupMap.putAll(m);
}

/**
* Add Attribute Group Mapping
* @param g AttributeGroup too Add
* @param id Id of the Attribute within the group
*/
public void addAttrGroup(AttributeGroup g, Integer id) {
if(log.isDebugEnabled()) log.debug("-->addAttrGroup:"+g.getGroupName()+" id:"+id);
attrGroupMap.put(g.getGroupId(),id);
if(log.isDebugEnabled()) log.debug("<--addAttrGroup()");
}

..........


}


Java code doing update/save (this is from spring)

public class ProductGroupDAOImpl extends HibernateDaoSupport implements ProductGroupDAO {
private static Log log = LogFactory.getLog(ProductGroupDAOImpl.class);

public void addProductGroup(ProductGroup group) throws Exception {
if(log.isDebugEnabled()) log.debug("-->addProductGroup()");
if(log.isDebugEnabled()) log.debug(group);
getHibernateTemplate().save(group);
if(log.isDebugEnabled()) log.debug("<--addProductGroup()");
}

public void updateProductGroup(ProductGroup group) throws Exception {
if(log.isDebugEnabled()) log.debug("-->updateProductGroup()");
if(log.isDebugEnabled()) log.debug("enter: "+group.dumpData());
getHibernateTemplate().update(group);
if(log.isDebugEnabled()) log.debug("leave: "+group.dumpData());
if(log.isDebugEnabled()) log.debug("<--updateProductGroup()");
}

public void updateProductGroupsInBatch(Collection c) throws Exception {
if(log.isDebugEnabled()) log.debug("-->updateProductGroupsInBatch()");
if(log.isDebugEnabled()) log.debug("Updating "+c.size()+" ProductGroups");
for(Iterator i = c.iterator();i.hasNext(); ) {
updateProductGroup((ProductGroup)i.next());
}
}
}

Debug trace

2004-07-28 15:14:37,768 DEBUG com.stormhq.entity.ProductGroupDAOImpl - -->updateProductGroup()
2004-07-28 15:14:37,768 DEBUG com.stormhq.entity.ProductGroupDAOImpl - enter: [Class [com.stormhq.entity.ProductGroup] getId[8157] getAttributeMappings[{15=29, 4=9, 19=37, 8=15, 11=21, 16=30, 18=34, 3=6, 7=14, 12=23, 22=142, 17=31, 13=25, 9=17, 21=41, 6=12, 1=1, 20=40, 14=27, 10=19, 5=10}] getAttrGroupMap[{15=29, 4=9, 19=37, 8=15, 11=21, 16=30, 18=34, 3=6, 7=14, 12=23, 22=142, 17=31, 13=25, 9=17, 21=41, 6=12, 1=1, 20=40, 14=27, 10=19, 5=10}] getCode1[ESKARP] getCode2[null] getListPrice[89.0] getLongDescription[ This stylish croco bootie has a side zip closure with a flexible rubber bottom for added comfort. 2" covered heel] getSaleString[null] getDisplayRank[75] getName[esKarp] getShortDescription[esKarp, bootie]]
ProductGroup has 21 attributes.AGroup: ----> 15 : 29AGroup: ----> 4 : 9AGroup: ----> 19 : 37AGroup: ----> 8 : 15AGroup: ----> 11 : 21AGroup: ----> 16 : 30AGroup: ----> 18 : 34AGroup: ----> 3 : 6AGroup: ----> 7 : 14AGroup: ----> 12 : 23AGroup: ----> 22 : 142AGroup: ----> 17 : 31AGroup: ----> 13 : 25AGroup: ----> 9 : 17AGroup: ----> 21 : 41AGroup: ----> 6 : 12AGroup: ----> 1 : 1AGroup: ----> 20 : 40AGroup: ----> 14 : 27AGroup: ----> 10 : 19AGroup: ----> 5 : 10
2004-07-28 15:14:37,769 DEBUG net.sf.hibernate.impl.SessionImpl - opened session
2004-07-28 15:14:37,769 DEBUG net.sf.hibernate.impl.SessionImpl - updating [com.stormhq.entity.ProductGroup#8157]
2004-07-28 15:14:37,769 DEBUG net.sf.hibernate.impl.SessionImpl - collection dereferenced while transient [com.stormhq.entity.ProductGroup.attrGroupMap#8157]
2004-07-28 15:14:37,769 DEBUG net.sf.hibernate.engine.Cascades - processing cascades for: com.stormhq.entity.ProductGroup
2004-07-28 15:14:37,769 DEBUG net.sf.hibernate.engine.Cascades - done processing cascades for: com.stormhq.entity.ProductGroup
2004-07-28 15:14:37,769 DEBUG net.sf.hibernate.impl.SessionImpl - flushing session
2004-07-28 15:14:37,769 DEBUG net.sf.hibernate.engine.Cascades - processing cascades for: com.stormhq.entity.ProductGroup
2004-07-28 15:14:37,769 DEBUG net.sf.hibernate.engine.Cascades - done processing cascades for: com.stormhq.entity.ProductGroup
2004-07-28 15:14:37,769 DEBUG net.sf.hibernate.impl.SessionImpl - Flushing entities and processing referenced collections
2004-07-28 15:14:37,769 DEBUG net.sf.hibernate.impl.WrapVisitor - Wrapped collection in role: com.stormhq.entity.ProductGroup.attrGroupMap
2004-07-28 15:14:37,769 DEBUG net.sf.hibernate.impl.SessionImpl - Updating entity: [com.stormhq.entity.ProductGroup#8157]
2004-07-28 15:14:37,769 DEBUG net.sf.hibernate.impl.SessionImpl - Collection found: [com.stormhq.entity.ProductGroup.attrGroupMap#8157], was: [<unreferenced>]
2004-07-28 15:14:37,769 DEBUG net.sf.hibernate.impl.SessionImpl - Processing unreferenced collections
2004-07-28 15:14:37,770 DEBUG net.sf.hibernate.impl.SessionImpl - Scheduling collection removes/(re)creates/updates
2004-07-28 15:14:37,770 DEBUG net.sf.hibernate.impl.SessionImpl - Flushed: 0 insertions, 1 updates, 0 deletions to 1 objects
2004-07-28 15:14:37,770 DEBUG net.sf.hibernate.impl.SessionImpl - Flushed: 1 (re)creations, 0 updates, 1 removals to 1 collections
2004-07-28 15:14:37,770 DEBUG net.sf.hibernate.impl.Printer - listing entities:
2004-07-28 15:14:37,770 DEBUG net.sf.hibernate.impl.Printer - com.stormhq.entity.ProductGroup{code2=null, saleString=null, displayRank=75, shortDescription=esKarp, bootie, listPrice=89.0, visible=false, attrGroupMap=[], name=esKarp, longDescription= This stylish croco bootie has a side zip closure with a flexible rubber bottom for added comfort. 2" covered heel, id=8157, code1=ESKARP}
2004-07-28 15:14:37,770 DEBUG net.sf.hibernate.impl.SessionImpl - executing flush
2004-07-28 15:14:37,770 DEBUG net.sf.hibernate.persister.EntityPersister - Updating entity: [com.stormhq.entity.ProductGroup#8157]
2004-07-28 15:14:37,770 DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
2004-07-28 15:14:37,770 DEBUG net.sf.hibernate.SQL - update Rick.PRODUCT_GROUP set code1=?, code2=?, display_rank=?, list_price=?, long_description=?, sale_string=?, name=?, short_description=?, visible=? where pg_id=?
Hibernate: update Rick.PRODUCT_GROUP set code1=?, code2=?, display_rank=?, list_price=?, long_description=?, sale_string=?, name=?, short_description=?, visible=? where pg_id=?
2004-07-28 15:14:37,770 DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
2004-07-28 15:14:37,772 DEBUG net.sf.hibernate.persister.EntityPersister - Dehydrating entity: [com.stormhq.entity.ProductGroup#8157]
2004-07-28 15:14:37,772 DEBUG net.sf.hibernate.type.StringType - binding 'ESKARP' to parameter: 1
2004-07-28 15:14:37,772 DEBUG net.sf.hibernate.type.StringType - binding null to parameter: 2
2004-07-28 15:14:37,772 DEBUG net.sf.hibernate.type.IntegerType - binding '75' to parameter: 3
2004-07-28 15:14:37,772 DEBUG net.sf.hibernate.type.FloatType - binding '89.0' to parameter: 4
2004-07-28 15:14:37,773 DEBUG net.sf.hibernate.type.StringType - binding ' This stylish croco bootie has a side zip closure with a flexible rubber bottom for added comfort. 2" covered heel' to parameter: 5
2004-07-28 15:14:37,773 DEBUG net.sf.hibernate.type.StringType - binding null to parameter: 6
2004-07-28 15:14:37,773 DEBUG net.sf.hibernate.type.StringType - binding 'esKarp' to parameter: 7
2004-07-28 15:14:37,773 DEBUG net.sf.hibernate.type.StringType - binding 'esKarp, bootie' to parameter: 8
2004-07-28 15:14:37,773 DEBUG net.sf.hibernate.type.BooleanType - binding 'false' to parameter: 9
2004-07-28 15:14:37,773 DEBUG net.sf.hibernate.type.IntegerType - binding '8157' to parameter: 10
2004-07-28 15:14:37,773 DEBUG net.sf.hibernate.impl.BatcherImpl - Adding to batch
2004-07-28 15:14:37,773 DEBUG net.sf.hibernate.impl.BatcherImpl - Executing batch size: 1
2004-07-28 15:14:37,928 DEBUG net.sf.hibernate.impl.BatcherImpl - success of batch update unknown: 0
2004-07-28 15:14:37,928 DEBUG net.sf.hibernate.impl.BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
2004-07-28 15:14:37,928 DEBUG net.sf.hibernate.impl.BatcherImpl - closing statement
2004-07-28 15:14:37,929 DEBUG net.sf.hibernate.collection.BasicCollectionPersister - Deleting collection: [com.stormhq.entity.ProductGroup.attrGroupMap#8157]
2004-07-28 15:14:37,929 DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
2004-07-28 15:14:37,929 DEBUG net.sf.hibernate.SQL - delete from Rick.prod_group_att_group_map where pg_id=?
Hibernate: delete from Rick.prod_group_att_group_map where pg_id=?
2004-07-28 15:14:37,929 DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
2004-07-28 15:14:37,931 DEBUG net.sf.hibernate.type.IntegerType - binding '8157' to parameter: 1
2004-07-28 15:14:37,932 DEBUG net.sf.hibernate.impl.BatcherImpl - Adding to batch
2004-07-28 15:14:37,932 DEBUG net.sf.hibernate.collection.BasicCollectionPersister - done deleting collection
2004-07-28 15:14:37,932 DEBUG net.sf.hibernate.impl.BatcherImpl - Executing batch size: 1
2004-07-28 15:14:37,935 DEBUG net.sf.hibernate.impl.BatcherImpl - success of batch update unknown: 0
2004-07-28 15:14:37,935 DEBUG net.sf.hibernate.impl.BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
2004-07-28 15:14:37,935 DEBUG net.sf.hibernate.impl.BatcherImpl - closing statement
2004-07-28 15:14:37,935 DEBUG net.sf.hibernate.collection.BasicCollectionPersister - Inserting collection: [com.stormhq.entity.ProductGroup.attrGroupMap#8157]
2004-07-28 15:14:37,935 DEBUG net.sf.hibernate.collection.BasicCollectionPersister - collection was empty
2004-07-28 15:14:37,935 DEBUG net.sf.hibernate.impl.SessionImpl - post flush
2004-07-28 15:14:37,936 DEBUG net.sf.hibernate.impl.SessionImpl - closing session
2004-07-28 15:14:37,936 DEBUG net.sf.hibernate.impl.SessionImpl - disconnecting session
2004-07-28 15:14:37,936 DEBUG net.sf.hibernate.impl.SessionImpl - transaction completion
2004-07-28 15:14:37,936 DEBUG com.stormhq.entity.ProductGroupDAOImpl - leave: [Class [com.stormhq.entity.ProductGroup] getId[8157] getAttributeMappings[{}] getAttrGroupMap[{}] getCode1[ESKARP] getCode2[null] getListPrice[89.0] getLongDescription[ This stylish croco bootie has a side zip closure with a flexible rubber bottom for added comfort. 2" covered heel] getSaleString[null] getDisplayRank[75] getName[esKarp] getShortDescription[esKarp, bootie]]
ProductGroup has 0 attributes.
2004-07-28 15:14:37,936 DEBUG com.stormhq.entity.ProductGroupDAOImpl - <--updateProductGroup()


Top
 Profile  
 
 Post subject: Solved
PostPosted: Thu Jul 29, 2004 10:35 am 
Newbie

Joined: Wed Jul 28, 2004 3:20 pm
Posts: 16
Here's the solution if anybody is interested. Someone on the spring list answered (although it's not a spring issue).

---
The thing is that Hibernate uses == to check collection identity.
Therefore copying maps using putAll is not the best idea:

public void setAttrGroupMap(Map m) {
attrGroupMap.clear();
attrGroupMap.putAll(m);
}

Replace this with something like
attrGroupMap = m;
and try again.


------


Rick


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.