• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate edit database record

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both way we can do update
 
Bart Hofma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bart Hofma, please post the error .
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic