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

identifier of an instance altered

 
Rachel George
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Person Class.to tht i'm setting some vales which gets saved in my database through hibernate.
then i used Criteria to load those values.it did perfectly fine.
then i gave person.setId(5) which was 4 earlier and then session.saveOrUpdate(person)
it's giving me error as
org.hibernate.HibernateException: identifier of an instance of Person was altered from 4 to 5

Why is it like that?I'm new to hibernate...

Regards
Rachel
 
saranga rao
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if the id is update existing state has a non-null id
if the id is save existing state has null id..

or may be You don't have the priviledges to alter the table

instead you can use update();
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Updating an object and changing the id is not really a valid thing to do in relational databases. The id uniquely identifies a record, if you change it it is analagous to deleting a record and inserting a new one. So, Hibernate will complain if you try to do this.
 
saranga rao
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dynamic-update: If true, then when Hibernate updates a row in the database for a class
attribute that has changed, it generates an UPDATE SQL statement at runtime during the
update() method call on the session and only include the columns of the table that have been
updated. This is an optional attribute; the default is false.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic