This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate update method

 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<property name="" column="" update="false">

The column specified wont be modified on an update.

-HTH
 
Bougnon Kipre
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way is to use update("entityname" , object);
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur JainZeroZeroSeven,
Please check your private messages.
-DOM
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic