Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reseting a db record with a new value, load() and update()

 
M Marin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have the following code. I am trying to update a db table by first loading the record and after setting it with the new value, but somehow I am the worst java programmer in the world. I want to leave the same value in the record if the argument passed in the form is null or otherwise set the record with the new value, but it does not work.

public void modifyAppInstallation(long lfdNr, String name) {

Intallations theNewInstallation = (Installations) sess().load(Installations.class, lfdNr);


if(name==null){
theNewInstallation.setName(theNewInstallation.getName());
}else {
theNewInstallation.setName(name);
}

sess().update(theNewInstallation);
}

Regards.

MR.
 
aruna sydu
Ranch Hand
Posts: 98
MyEclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Marin,

To Solve the problem,Go 1 step at a time.

1.Loading the theNewInstallation object.
Are you loading the currect object from the database for which you are trying to update?
For this check/print the primary key that you are passing and also the theNewInstallation that is being returned.
You can check by simple println statement or by debuggin the values.

2.the name that you are trying to update the theNewInstallation object.
Is the name value null or empty name.length===0 ? should also be checked if you are not sure of the value name.

3.No need to update the theNewInstallation object when the name is null because it already contains value

4.When you are calling the update method on the seession object , turn on <property name="show_sql">true</property> in hiberanate.cfg.xml
This will show you the update query fired by hibernate ,so that you can check where you are going wrong.




Thanks ,
Aruna


 
M Marin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Aruna,

I was getting the wrong value of name.

Regards.

MR
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic