-->
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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate 'Data update' query
PostPosted: Thu Mar 09, 2006 9:40 am 
Newbie

Joined: Mon Dec 19, 2005 8:33 am
Posts: 8
Hi ,


We are using hibernate as our Data Access Layer in a Service Oriented environment.

I have a query regarding the 'data update' in hibernate.

How do we update a 'detached' object in hibernate based on criteria other than 'primary key' without first retrieving the object ?

Ex:
I need to update a single user attribute 'attr1' based on the 'username' as criteria.

Typically to do this i need to first get the entire user object and then set the new attribute value as session.update updates only by primary key.

But I would not want to fire a query to get the whole user object to update a single attribute.

Is it possible inherently with hibernate without writing a query ?

Any help would be highly appreciated.

Thanks,
Ram


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 12:49 pm 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
You could use a HQL update like that:

Code:
Query query = session.createQuery("update User set name = 'tom' where id = 1");
query.executeUpdate();


simon


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 11:49 pm 
Newbie

Joined: Mon Dec 19, 2005 8:33 am
Posts: 8
Yes , i know that we can write a HQL query and do that.

Ok, My intention was to write a dynamic query and update only those attributes that are changed using dirty checking .

somthing like this


public void update(User user,User criteria)
{

String sql =" update users set ";

if(user.isDirty(attr1))
{
sql += " attr1 = "+ user.getAttr1();
}

if(user.isDirty(attr2))
{
sql += " attr2 = "+ user.getAttr2();
}

....

sql +=" where user.getUserName()";

}

My question is if i can somehow use dirty checking of hibernate or do i need to implement my own dirty checking here?


Thanks in advance
Ram


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 5:30 am 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
The dirty checking works quiet well within Hibernate but if you work with detached objects you have to reassociate the detached object with the session to enable dirty checking again. So you need to use session.update / session.merge to associate your object with the session again

you can use the dynamic-update behaviour for your classes in your hbm.xml:

<class dynamic-update="true"...

dynamic-update (optional, defaults to false): Specifies that UPDATE SQL should be generated at runtime and contain only those columns whose values have changed.


simon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 10:53 am 
Newbie

Joined: Mon Dec 19, 2005 8:33 am
Posts: 8
Hi Simon,

Thanks for the reply.

The problem here is :

To reassociate the object to session i think we need to set the primary key of the object.

To get the primary key (id) I need to retrive the user object , as i only get the logical identifier 'username' as input to my request.

My whole idea, was to avoid retrieving a huge object to just update one or two user attributes.

To sum it up:

I want to execute a query like this: (without a additional select query to retrieve the id )

update user set attr1=value where username=?

I know this can be done using HQL.
But i want to generate the 'set' part in update query dynamically.For this
i want to see if i can anyway use/intercept hibernate dirty checking ?

Thanks for your patience.

Ram


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