Hibernate version: 3.1
Mapping documents:
Code:
<class name="User">
<id name="id" column="user_id">
<generator class="native"/>
</id>
<version name="version" access="field"/>
<property name="name"/>
<set name="userPermissions" inverse="true" cascade="delete">
<key column="user_id"/>
<one-to-many class="UserPermissions"/>
</set>
</class>
<class name="UserPermissions">
<id name="id" column="userpermissions_id">
<generator class="native"/>
</id>
<many-to-one name="user" column="user_id"/>
<many-to-one name="device" column="device_id"/>
<many-to-one name="group" column="usergroup_id"/>
</class>
Issue:When I do the following query to find a user by his/her name:
Code:
Query q = session.createQuery("from User where name = :userName")
.setString("userName", username);
User user = (q.list().size() > 0) ? (User)q.list().get(0) : null;
I get the following SQL's:
Code:
Hibernate: select user0_.user_id as user1_, user0_.version as version2_, user0_.name as name2_ from User user0_ where user0_.name=?
Hibernate: select userpermis0_.user_id as user4_1_, userpermis0_.userpermissions_id as userperm1_1_, userpermis0_.userpermissions_id as userperm1_0_, userpermis0_.device_id as device2_4_0_, userpermis0_.usergroup_id as usergroup3_4_0_, userpermis0_.user_id as user4_4_0_ from UserPermissions userpermis0_ where userpermis0_.user_id=?
Hibernate: update User set version=?, name=? where user_id=? and version=?
Hibernate: select user0_.user_id as user1_, user0_.version as version2_, user0_.name as name2_ from User user0_ where user0_.name=?
Why is Hibernate doing an unneccessary update on a find? I note that if I take the automatic versioning out, then there is no update.