-->
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.  [ 3 posts ] 
Author Message
 Post subject: why can't I 'update' in the way 'select' does?
PostPosted: Sat Feb 12, 2011 11:53 am 
Newbie

Joined: Sat Feb 12, 2011 11:34 am
Posts: 2
I find that Hibernate supports casecade query using HQL.

for instance, we have two classes:
1) School, which has properties: Long id and String name
2) User, which has properties: Long id, String name and School school

we can query by executing "select name,school.name from User" to get a user's name and school name.

but we can't execute "update User set name='xxx' , school.name='yyy' where id=zzz".

I use update in HQL because it can only update the non-null properties.

Is there a way to do this?


Top
 Profile  
 
 Post subject: Re: why can't I 'update' in the way 'select' does?
PostPosted: Sat Feb 12, 2011 1:13 pm 
Newbie

Joined: Sun May 09, 2010 4:48 am
Posts: 11
Why don't you do it like this?
load User
User setName ("xxx")
User setSchool(new School("yyy"))
save User


Top
 Profile  
 
 Post subject: Re: why can't I 'update' in the way 'select' does?
PostPosted: Sat Feb 12, 2011 9:35 pm 
Newbie

Joined: Sat Feb 12, 2011 11:34 am
Posts: 2
mueven wrote:
Why don't you do it like this?
load User
User setName ("xxx")
User setSchool(new School("yyy"))
save User

It can work, but executing "Load User" will cost unnecessary time.

Because in that way, hibernate will update the null value and clear the original data, say we have property "String address" for School, I want to remain address as it is and only update user.name and school.name.

HQL update can only update the fields we SET and keep others no changed. Thanks for reply.


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