wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Hibernate update method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate update method" Watch "Hibernate update method" New topic
Author

Hibernate update method

Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Hi All,

I have a table having column id,password ,username.I have an object naming User (POJO) . I have set username in object User.Now if i use hibernate update(UserObj) , it makes password value blank in the table. is there any way so that old value in the table can be retained. is there some property that can be set in hbm so that if an update happens then a particular column doesnt update.

Please give suggestions on this.Thanks a lot
suj hande
Greenhorn

Joined: Sep 07, 2007
Posts: 7
I think in the mapping of class there is a default-update or update element which you can set to achieve what you are looking for.
Ranjit Gopinathan
Ranch Hand

Joined: Jul 03, 2007
Posts: 46
<property name="" column="" update="false">

The column specified wont be modified on an update.

-HTH
Bougnon Kipre
Greenhorn

Joined: Sep 13, 2007
Posts: 21
Originally posted by Ranjit Gopinathan:
<property name="" column="" update="false">

The column specified wont be modified on an update.

-HTH


This would work.
However, why would somebody want to do this at all?
Unless business rules call to never modify the password after it is initially set.
Bougnon Kipre
Greenhorn

Joined: Sep 13, 2007
Posts: 21
One more thing...
If you go the route suggested by Ranjit Gopinathan, maybe you should look into not exposing the password field to the user on update.
That way, it won't give the false sense that this filed is modifiable via the application.
[ September 14, 2007: Message edited by: Bougnon Kipre ]
Ranjit Gopinathan
Ranch Hand

Joined: Jul 03, 2007
Posts: 46
I agree with Bougnon Kipre.

My intention was to answer this question.

Originally posted by Tweety Samuel:
Hi All,

I have a table having column id,password ,username.I have an object naming User (POJO) . I have set username in object User.Now if i use hibernate update(UserObj) , it makes password value blank in the table. is there any way so that old value in the table can be retained.
------
is there some property that can be set in hbm so that if an update happens then a particular column doesnt update.
---------

Please give suggestions on this.Thanks a lot

[ September 14, 2007: Message edited by: Ranjit Gopinathan ]
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Thanks to all!!!

if i want to update a field when object has set that field otherwise retain the old value.
Suppose UserObject contain userName field set at one time then table will be update with this userName. But other time this value is not set in this case table must retain old value.This is just an example, no business logic here.
so in this case update=false would not work as it will be applied everytime.it would not let the user update the table.

Please help me!! or please tell me if i am not able to make my query clear
Thanks a lot
Ranjit Gopinathan
Ranch Hand

Joined: Jul 03, 2007
Posts: 46
Am not an expert with hibernate .Correct me if am wrong

This is what i do for a partial update of a row.

An hql query like :

UPDATE User SET username='"+ <user name> +"' WHERE id= :userId");

query..setInteger("userId", <user id> ;

execute the HQL Query.
Bougnon Kipre
Greenhorn

Joined: Sep 13, 2007
Posts: 21
Let me understand this.
When you retrieve the object from the database, you should have all the field
loaded with their respective persistent values, therefore in your hibernate,
session , password should also have its persistent value.

If the password value was not changed by the user interface, then the same value (non-null) should still be available when you update.

In this case, update="true" should remain in your hbm for password field.

Something seems to be missing in your explanation.

Maybe somewhere in your logic, you are inadvertently setting the password

field to null after loading your POJO object from the database.
[ September 14, 2007: Message edited by: Bougnon Kipre ]
Bougnon Kipre
Greenhorn

Joined: Sep 13, 2007
Posts: 21
If you are not sure you changed the password in your logic,
please try to add to your class tag in the hbm, the following attribute and value



This will ensure that only changed fields are included in your update statement. If you see password field in your update statement, that would mean that you are modifying this field.
[ September 17, 2007: Message edited by: Bougnon Kipre ]
Ankur Jain
Greenhorn

Joined: Sep 09, 2008
Posts: 6
The best way is to use update("entityname" , object);
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Ankur JainZeroZeroSeven,
Please check your private messages.
-DOM
Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
What if in the same case, while selecting, i selected only few fields and not all fields of a POJO.. Now i want to update them..
Gopal Krishan
Greenhorn

Joined: Aug 11, 2010
Posts: 1
I had faced the same problem.. my fault was i was updating on a new object rather then the object on with i have to update.
So first get that object which you want to update with its primary key and then set the desired values and then do the session update.


Gopal K Sharma
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Hibernate update method