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 (?, ?, ?)
|