File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes identifier of an instance altered Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "identifier of an instance altered" Watch "identifier of an instance altered" New topic

identifier of an instance altered

Rachel George

Joined: May 03, 2007
Posts: 1
I have a Person tht i'm setting some vales which gets saved in my database through hibernate.
then i used Criteria to load those 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...

saranga rao
Ranch Hand

Joined: Apr 24, 2007
Posts: 49
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

Joined: Apr 14, 2004
Posts: 10336

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.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
saranga rao
Ranch Hand

Joined: Apr 24, 2007
Posts: 49
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.
I agree. Here's the link:
subject: identifier of an instance altered
It's not a secret anymore!