-->
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: Save() doesn't work properly with map, update fixes
PostPosted: Mon Feb 19, 2007 2:44 am 
Newbie

Joined: Thu Feb 15, 2007 5:46 am
Posts: 12
Location: Helsinki
Session.save() does not save values properly to the link table for a hashmap. The generated SQL shows that values are saved to the parent object's table and child object's table, but not the corresponding link table.

If I do a merge after the save, the link table is updated. But for obvious reasons I wouldn't like to do a save/merge every time I wish to save an object.

I seriously doubt that this is intended functionality, since if you simply save() an object and then reload it, the associated map would be empty due to the link table missing the entries.

Regards,
Jussi

Hibernate version: 3.2

Mapping documents:
<hibernate-mapping default-lazy="false">
<class name="com.digitalchocolate.ddb.domain.test.TestParentObject" table="test_parent">
<id name="id" column="id" type="long" unsaved-value="null">
<generator class="native"/>
</id>
<timestamp name="version" column="hibernate_version" source="db"/>

<property name="parentId" column="parent_id" type="long"/>
<map name="children" table="test_par_chld" cascade="all">
<key column="parentid"/>
<map-key type="string" column="childname" length="20"/>
<many-to-many column="child_id" class="com.digitalchocolate.ddb.domain.test.TestChildObject"/>
</map>

</class>
</hibernate-mapping>

<hibernate-mapping default-lazy="false">
<class name="com.digitalchocolate.ddb.domain.test.TestChildObject" table="test_child">
<id name="id" column="id" type="long" unsaved-value="null">
<generator class="native"/>
</id>
<timestamp name="version" column="hibernate_version" source="db"/>

<property name="parentId" column="parent_id" type="long"/>

<property name="name" type="string"/>
<property name="age" type="integer"/>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
For save:
session.save(object);
session.evict(object);
t.commit();

Name and version of the database you are using: MySQL 5.0.22

The generated SQL (show_sql=true):
Hibernate: select now()
Hibernate: insert into test_parent (hibernate_version, parent_id) values (?, ?)
Hibernate: select now()
Hibernate: insert into test_child (hibernate_version, parent_id, name, age) values (?, ?, ?, ?)
Hibernate: select now()
Hibernate: insert into test_child (hibernate_version, parent_id, name, age) values (?, ?, ?, ?)
Hibernate: select now()
Hibernate: insert into test_child (hibernate_version, parent_id, name, age) values (?, ?, ?, ?)
Hibernate: select now()
Hibernate: insert into test_child (hibernate_version, parent_id, name, age) values (?, ?, ?, ?)

Doing merge:
Hibernate: update test_parent set hibernate_version=?, parent_id=? where id=? and hibernate_version=?
Hibernate: insert into test_par_chld (parentid, childname, child_id) values (?, ?, ?)
Hibernate: insert into test_par_chld (parentid, childname, child_id) values (?, ?, ?)
Hibernate: insert into test_par_chld (parentid, childname, child_id) values (?, ?, ?)
Hibernate: insert into test_par_chld (parentid, childname, child_id) values (?, ?, ?)


Top
 Profile  
 
 Post subject: Okay, problem solved
PostPosted: Mon Feb 19, 2007 6:22 am 
Newbie

Joined: Thu Feb 15, 2007 5:46 am
Posts: 12
Location: Helsinki
I managed to solve my problem finally. It seems that evicting the object after save() and before commit() causes the cascading to break.

Or if this is intentional, that's one hell of a side effect. I gotta say I'm a bit disappointed in Hibernate. I wasted 10 hours to track a versioning bug, which was due to hibernate creating a datetime out of <version type="timestamp"> instead of a timestamp (with mysql). And finally I managed to find a bug report on it, where Gavin declared it not to be a bug. Well Gavin, if it's not a bug, it's at least a horrible feature.

Maybe I can get going with my work now that my battle with hibernate seems to be turning for the better.

Bitter regards,
Jussi


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 6:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
thinking that hibernate can perform flush time cascades on objects that you have evicted before commit so it does not know anything about the object when flush occurs sounds a tad too optimistic does it not ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 6:52 am 
Newbie

Joined: Thu Feb 15, 2007 5:46 am
Posts: 12
Location: Helsinki
max wrote:
thinking that hibernate can perform flush time cascades on objects that you have evicted before commit so it does not know anything about the object when flush occurs sounds a tad too optimistic does it not ?


Well, I find it odd that with the evict() the cascade does occur, since the parent object and the child objects are saved. But the link table remains empty. I admit I don't know very much how hibernate handles the cache, but I would've expected an exception or something, instead of having objects persisted but the references between them not.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 7:11 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
well you have used an id strategy that requires the object to be saved instantly; certain cascade's won't be cascaded until flush time.

All documented (and I would say quite reasonable)

_________________
Max
Don't forget to rate


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.