-->
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.  [ 6 posts ] 
Author Message
 Post subject: Unexpected update with automatic versioning
PostPosted: Thu Jan 19, 2006 3:20 pm 
Beginner
Beginner

Joined: Tue Jun 14, 2005 12:14 pm
Posts: 37
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.


Top
 Profile  
 
 Post subject: Re: Unexpected update with automatic versioning
PostPosted: Thu Jan 19, 2006 3:41 pm 
Beginner
Beginner

Joined: Tue Jun 29, 2004 12:35 pm
Posts: 21
liem wrote:
Hibernate version: 3.1

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;




Dunno about update, but does this also update?

List lst = q.list();
User user=(lst.isEmpty() ? null : lst.get(0));


Top
 Profile  
 
 Post subject: Re: Unexpected update with automatic versioning
PostPosted: Thu Jan 19, 2006 3:58 pm 
Beginner
Beginner

Joined: Tue Jun 14, 2005 12:14 pm
Posts: 37
drooler wrote:
List lst = q.list();
User user=(lst.isEmpty() ? null : lst.get(0));


That does not generate an update! But the following does:
Code:
User user = q.list().isEmpty() ? null : q.list().get(0);


Why is that the case? I am obviously newbie on this topic! BTW, thanks drooler! I incremented 1 point for ya!

Liem


Top
 Profile  
 
 Post subject: Re: Unexpected update with automatic versioning
PostPosted: Thu Jan 19, 2006 4:03 pm 
Beginner
Beginner

Joined: Tue Jun 29, 2004 12:35 pm
Posts: 21
liem wrote:
drooler wrote:
List lst = q.list();
User user=(lst.isEmpty() ? null : lst.get(0));


That does not generate an update! But the following does:
Code:
User user = q.list().isEmpty() ? null : q.list().get(0);


Why is that the case?


I suspect that cause is that you are accessing same Hibernate Query object twice, I dont know why it updates row, maybe it is on docs (I never have accessed same instance of Query as your example did :D

Anybody more familiar on subject can enlight freely :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 5:51 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Do you have any code that would make hibernate think that the user object loaded during the first qry.list() has been dirtied? Maybe a PostLoadEventListener? Or even some mutator/setter code that modifies the values as they arrive from the result set? Maybe the User class has something like this?
Code:
void setName(String n) { this.name = "'" + n + "'"; }


BTW, your ternary operator could be replaced by
Code:
User user = qry.uniqueResult();
That's much more readable and definitely avoids the spurious update.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 2:34 pm 
Beginner
Beginner

Joined: Tue Jun 14, 2005 12:14 pm
Posts: 37
I don't have a post listener or mutator in the set methods. It is also a brand-new session in an isolated JUnit test, so it could not have been dirtied.

BTW, uniqueResult() would not work, because if I get back more than 1 item, I will get a HibernateException, which I don't want. Thanks for the tip, though!


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