• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate edit database record

 
Bart Hofma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i have been trying to get this working all day but i cant figure it out. I really just need a simple example so i can fix this program at work.
i have no problem inserting new records using hibernate, but editing one keeps throwing errors.

here a hypothetical situation :
- lets assume there is already a working session
-there is a table called Person
-this table contains 2 fields, ID (which is primary key) and name
- lets say i want to update the record where ID = 100

how should i do that? i am trying the following, but it just wont work

String query= "from Person where ID = 100";
Person p1 = (Person) sessionFactory.getCurrentSession().createQuery(query);
String name="Why Wont It Work" ;
p1.setName(name);
sessionFactory.getCurrentSession().saveOrUpdate(best);

am i making mistakes here? or are these lines correct and should i look at other parts of my code

 
Madhavi Subramaniam
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bart,

Can you try like this

String query="update Person p1 set p1.name= :newName where p1.ID=:personId";

String name="Why Wont It Work" ;

int updatedEntities =sessionFactory.getCurrentSession().createQuery(query) .setString( "newName", name ).setInteger("personId",100) .executeUpdate();


Thanks


 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if i load the data with ID = 100, (using load method) and set the new value of the property and update it using "saveOrUpdate". This way i don't need to embedd the SQL query
 
Madhavi Subramaniam
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both way we can do update
 
Bart Hofma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal Lakhani wrote:What if i load the data with ID = 100, (using load method) and set the new value of the property and update it using "saveOrUpdate". This way i don't need to embedd the SQL query


This seems interesting, i will try this solution today. I'm implementing it in a more complex situation, so it might take some time, but ill let you know if it works out.
 
Bart Hofma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, so the load thing is too complicated for me. guess im too noob at this to try that.

the update method seems to work, but now im getting a tottally different error, maybe someone has an idea on this.

i want to update a field where ID=002000048953523 (its a string in my code and in database also) , the error is 'unexpected token 953523'.

what is this about?
 
Vijitha Kumara
Bartender
Pie
Posts: 3930
21
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bart Hofma wrote:...the update method seems to work, but now im getting a tottally different error, maybe someone has an idea on this...

Where in what code you get this problem. You need to explain the scenario bit more (TellTheDetails)
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bart Hofma, please post the error .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic