Hello,
using Hibernate 3.5.2-Final.
The problem (i believe it's a bug) is that hibernate will cause an update to the version when using merge, even when the object has not changed. The issue occurs when replacing a Set reference with a new HashSet in the loaded object.
Here is the mapping to test:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="scanserver.util.hibernate.tests">
<class name="GroupTest" table="groups" lazy="false" dynamic-update="true">
<id column="id" name="id">
<generator class="native"/>
</id>
<version column="version" name="version"/>
<property name="name"/>
</class>
<class name="UserTest" table="users" lazy="false" dynamic-update="true">
<id column="id" name="id"><generator class="native"/></id>
<version column="version" name="version"/>
<property name="userName"/>
<property name="name"/>
<set cascade="merge,save-update" fetch="join" name="Groups" table="usersgroups">
<key column="UserID"/>
<many-to-many class="GroupTest" column="GroupID"/>
</set>
</class>
</hibernate-mapping>
Here is the code to reproduce:
Code:
Session s = getSession();
s.beginTransaction();
UserTest u = (UserTest)s.load(UserTest.class, 1);
s.getTransaction().commit();
s.close();
//this is the line that causes the problem
u.setGroups(new HashSet(u.getGroups()));
s = getSession();
s.beginTransaction();
s.merge(u);
s.getTransaction().commit();
s.close();
Hibernate executes the following SQL:
Code:
Hibernate:
update
users
set
version=?
where
id=?
and version=?
Obviously, there is nothing to update here apart from the version!
If the line that replaces the Set is omited, then this update does not occur.
Costas