-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 posts ] 
Author Message
 Post subject: identifier of an instance [bean] was altered from <x>
PostPosted: Fri Oct 20, 2006 2:08 pm 
Newbie

Joined: Fri Oct 20, 2006 1:50 pm
Posts: 6
I am relatively new to Hibernate so I have been trying a VERY simple test, running on Tomcat 5.5.

I have a 'project' table in MS SQL Server 2005 which has a project_id primary key. This is an identity column. Other tables reference this column as a foreign key.

I created a ProjectBean class which contains this relevant code:

Code:
   private long id = 0;

   public ProjectBean()
   {
   }

   public long getId() {
      return id;
   }

   public void setId(long Id) {
      this.id = id;
   }


The relevant part of my ProjectBean.hbm.xml file looks like this:

Code:
          <id column="project_id" name="id" type="long">
               <generator class="identity"/>
          </id>


I have a simple jsp page which contains this scriptlet code:

Code:
SessionFactory sessionFactory = null;
Session hibSess = null;
Transaction tx = null;

try
{
sessionFactory = new Configuration().configure().buildSessionFactory();
hibSess = sessionFactory.openSession();
tx = hibSess.beginTransaction();
ProjectBean project = new ProjectBean();
project.setXXXX("xxxx");
hibSess.saveOrUpdate(project);
tx.commit();
}
catch(Exception e)
{
//** I get an exception right here, when saveOrUpdate() is called
tx.rollback();
}
finally
{
   if(hibSess != null)
   {
      hibSess.close();
   }
}



When I run this code I get the following error:

Quote:
org.hibernate.HibernateException: identifier of an instance of package.ProjectBean was altered from 1 to 0


I have tried everything I can think of to fix this. I changed the id variable from a long to a Long. In that case, my error simply stated that the value was altered from a 1 to null. I changed the generator from identity to native, with the same error. I made the project_id column in the table no longer an identity column, but then I got an error that a value was missing on the insert. I also tried to remove the primary-key/foreign key relationship between the project table and the other tables. No change. I used session.save() instead of session.saveOrUpdate(). No change.

If I reload the jsp page, the error changes by incrementing the value altered (from 2 to 0, from 3 to 0, etc.). If I change the default value of id in ProejctBean from 0 to another value (say, -1), then on the insert the instance gets altered instead to the new value (-1), not 0:

Quote:
org.hibernate.HibernateException: identifier of an instance of package.ProjectBean was altered from 1 to -1


I have seen A LOT of posts on this forum and throughout the web where people encountered a similar issue but I have not seen any good responses. Indeed, there has been no response to most of the posts in this forum on the matter.

Can someone please give me a definitive cause for this problem. I can't imagine an example that is more simple than this, so if it does not work, there must be a simple way to fix it.

Thanks.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 20, 2006 4:02 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
When you have
Code:
project.setXXXX("xxxx");
in your jsp, one of these methods is not project.setId(), is it? Also, for SQL Server, generator=native and generator=identity is equivalent. Also, try explicitly setting the unsaved-value=0 in the <id> element.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 20, 2006 4:13 pm 
Newbie

Joined: Fri Oct 20, 2006 1:50 pm
Posts: 6
i just used project.setXXX() to denote that properties were being set. I do not call setId() explicitly.

I added in unsaved-value="0", but that did not make a difference.

As MANY people have encountered this same problem and it seems fairly fundamental to getting hibernate to work (for those of us with problems, at least), further help on this would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 20, 2006 4:28 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I'm a bit busy for the next several hours, but I'll take another look at this tonight or tomorrow. By the way, the code in the DefaultFlushEntityEventListener.checkId() looks like it is swapping the "from" and "to" in the message, so the first message should read

Code:
org.hibernate.HibernateException: identifier of an instance of package.ProjectBean was altered from 0 to 1

which, of course, would make more sense.


Top
 Profile  
 
 Post subject: same problem
PostPosted: Fri Oct 27, 2006 12:52 pm 
Newbie

Joined: Fri Oct 27, 2006 12:48 pm
Posts: 2
Is there a solution for this? Did you guys find the problem? Could you help me?

I'm having this problem too:

Code:
org.hibernate.HibernateException: identifier of an instance of obj.Trade was altered from 198 to 199
Line 51 - org.hibernate.event.def.DefaultFlushEntityEventListener.checkId
Line 150 - org.hibernate.event.def.DefaultFlushEntityEventListener.getValues
Line 106 - org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity
Line 195 - org.hibernate.event.def.AbstractFlushingEventListener.flushEntities
Line 76 - org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions
Line 26 - org.hibernate.event.def.DefaultFlushEventListener.onFlush
Line 980 - org.hibernate.impl.SessionImpl.flush


Top
 Profile  
 
 Post subject: A solution PLEASE
PostPosted: Fri Oct 27, 2006 2:13 pm 
Newbie

Joined: Fri Oct 20, 2006 1:50 pm
Posts: 6
I've been a big fan of the hibernate project for a long time (although not a user until recently), so I am that much more disappointed that I have seen so many unanswered posts about this same problem.

if there is an experienced person on the hibernate development team who could give us an answer, it would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 4:19 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I have not been able to duplicate this error. Can you post enough info for me to be able to generate this error? Slim down the code to an absolute minimum.


Top
 Profile  
 
 Post subject: slimmed down code
PostPosted: Fri Oct 27, 2006 4:28 pm 
Newbie

Joined: Fri Oct 20, 2006 1:50 pm
Posts: 6
i'm not sure if it's possible to slim down the code more than I already have. look at the original posting. there's only about 20 lines of code and 3 lines from the mapping file. if that is not slim enough, i'm not sure how to proceed.


Top
 Profile  
 
 Post subject: I found it
PostPosted: Fri Oct 27, 2006 8:36 pm 
Newbie

Joined: Fri Oct 27, 2006 12:48 pm
Posts: 2
The problem with my code, I think it was because I had more than one thread altering the same objects...


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 08, 2007 6:50 pm 
Newbie

Joined: Tue May 08, 2007 6:41 pm
Posts: 1
Location: New Jersey, USA
I just resolved this problem, the issue was that I had the wrong data type for my primary key in the entity's .hbm.xml file.

The underlying database table (SQL Server) has a primary key column of bigint. This column is also an identity field.

My original mapping config looked like this:
Code:

<id
    name="recordId"
    type="int"
    column="record_id"
    unsaved-value="0">
    <generator class="identity" />
</id>


with this configuration I received the exception
identifier of an instance of ...[class name]... was altered from 1670012 to 1670012

I modified the mapping config to be:
Code:

<id
    name="recordId"
    type="long"
    column="record_id"
    unsaved-value="0">
    <generator class="identity" />
</id>


and it works now.


Top
 Profile  
 
 Post subject: Re:
PostPosted: Mon Aug 03, 2009 9:02 am 
Newbie

Joined: Fri Feb 08, 2008 6:53 am
Posts: 4
I had the same issue.

In my circumstance, I was using "assigned" primary keys (i.e. not auto-generated ones). If a child record was added to the collection of a parent before the primary key value had been assigned, I would get this error when saving the parent (which had a cascade action to save its child entries).

The solutiuon was to ensure that the primary key value was assigned to the child before adding the child to the parent's collection.


Top
 Profile  
 
 Post subject: Re: identifier of an instance [bean] was altered from <x>
PostPosted: Tue Feb 09, 2010 1:00 am 
Newbie

Joined: Thu Sep 10, 2009 6:21 pm
Posts: 4
Location: Almaty
Same issue! Expert could you answer the question!
https://forum.hibernate.org/viewtopic.php?f=1&t=999557


Top
 Profile  
 
 Post subject: Re: identifier of an instance [bean] was altered from <x>
PostPosted: Tue Mar 02, 2010 7:24 am 
Newbie

Joined: Tue Mar 02, 2010 7:19 am
Posts: 1
plz use this in the hbm.xml file for id generator :

<generator class="native" />

The generator causes a conflict in the Auto Increment option of MYSQL and Hibnerate.
Hope this will resolve the problem.


Abdullah Wasi


Top
 Profile  
 
 Post subject: identifier of an instance of Address was altered from 2 to 3
PostPosted: Thu Nov 17, 2011 9:21 am 
Newbie

Joined: Mon May 09, 2011 1:06 am
Posts: 4
I am having same problem.
I have Student object which holds reference to ID of Address object.
I update Student object and want to change the reference to another instance of Address Object. Say want to change ID from 2 to 3.
Hibernate throws exception
org.hibernate.HibernateException: identifier of an instance of Address was altered from 2 to 3
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:58)
at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:164)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:120)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:343)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)

The mappings I have are
there is no cascade here, so i am updating the parent , but updating reference in child.

In the session I do
start transaction
studentObject = session.get(StudentClass, id);
and update the AddressID from 2 to 3
call session.saveOrUpdate(StudentClass, studentObject);
and commit Transaction

<class name="com.san.Student" table="Student" dynamic-insert="true" mutable="true" polymorphism="implicit" dynamic-update="false" select-before-update="false" optimistic-lock="version">
<id name="ID" type="java.lang.Long" column="ID">
<generator class="native"/>
</id>
<many-to-one name="StudentAddress" name="com.san.Address" fetch="join" unique="true" update="true" insert="true" optimistic-lock="true" not-found="exception" embed-xml="true">
<column name="AddressID" not-null="true"/>
</many-to-one>
<property name="Name" type="java.lang.String" column="Name" unique="false" optimistic-lock="true" lazy="false" generated="never"/>
<property name="LastName" type="java.lang.String" column="Description" unique="false" optimistic-lock="true" lazy="false" generated="never"/>
</class>

<class name="com.san.Address" table="Address" dynamic-insert="true" mutable="true" polymorphism="implicit" dynamic-update="false" select-before-update="false" optimistic-lock="version">
<id name="ID" type="java.lang.Long" column="ID">
<generator class="native"/>
</id>

<property name="Add1" type="java.lang.String" column="Name" unique="false" optimistic-lock="true" lazy="false" generated="never"/>
<property name="Add2" type="java.lang.String" column="Description" unique="false" optimistic-lock="true" lazy="false" generated="never"/>
</class>


What could be the problem?


Top
 Profile  
 
 Post subject: Re: identifier of an instance [bean] was altered from <x>
PostPosted: Sat Dec 31, 2011 1:29 pm 
Newbie

Joined: Thu Aug 12, 2010 6:23 am
Posts: 2
Hi All,

I have faced this issue quite recently and the only thing I did wrong was, I was declaring the type of the identity column as "int" in hbm files.

I have changed it to LONG and everything is working fine.


Hope this helps someone !!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.