-->
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: update select columns
PostPosted: Wed Oct 15, 2008 12:25 pm 
Newbie

Joined: Wed Oct 15, 2008 11:51 am
Posts: 3
I have the following scenario :

table Car has 5 colums
1) id (PK)
2) name
3) speed
4) mileage
5) price

In my DAO I do something like :

Car c = new Car(); ( I am not loading this object from database)
car.setId(1);
car.setName("test");
session.update(car);

Is there a way to update only the name field instead of updating all the columns?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 1:48 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I have not done this to entities that did not come from the database to begin with, but maybe the following may work.

First, you have to set <class ... dynamic-update="true"> for your Car class.
Then try the following:

Code:
Car c = new Car();
car.setId(1);
session.lock(car, LockMode.NONE);
car.setName("test");
session.getTransaction().commit();


The call to Session.lock() should set the "zero"-point for your Car object. Everything you do to it after that is detected and the dynamic-update="true" setting should make sure that only changed values are sent to the database.

You might also simply consider doing a HQL UPDATE query:

Code:
update Car set name='test' where id=1


Top
 Profile  
 
 Post subject: it works
PostPosted: Wed Oct 15, 2008 2:22 pm 
Newbie

Joined: Wed Oct 15, 2008 11:51 am
Posts: 3
hi,

that works like a charm :) ...yeah right now I am firing a direct update sql for this ... but the number of fields that will be changed is going to be dynamic ..


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.