Techspace

IT happens only in IT

Hibernate Error – Identifier of an instance altered from 1 to 1

Problem:-Getting the error ” identifier of an instance of com.sample.db.Sample altered from 1 to 1″

Solution:- Check whether the java type of the identifier field in the Data Object, the type of the field in the hibernate xml file and the type of the column in the database are compatible types or not.

Explanation:- I had faced this strange problem today. I am using struts, hibernate and spring.

I found out that the problem roots from

public void

checkId(Object object, EntityPersister persister, Serializable id, EntityMode entityMode) method of DefaultFlushEntityEventListener class.

I solved this problem by changing the type of the id field in the class and the hbm.xml file.

We made some mistakes in declaring getter and setter methods and id field for this object. (therefore it is advisable to use Hibernate tools or other reverse engineer tools to generate your data object and hbm files)

The sample class was as follows

class Sample{

integer sampleId;

String sampleName;

public int getSampleId(){

….

}

public void setSampleId(int i){

…..

}

}

And this was the hbm file declaration

<id
name=”sampleId”
type=”integer”
column=”sample_id”
unsaved-value=”0″>
<generator class=”identity” />
</id>

<property
name=”sampleName”
type=”string”
column=”sample_name”
length=”50″
not-null=”true”
unique=”true”
/>

The problem was that in the database the type of the field was smallint which corresponds to the short data type in Java. Therefore somewhere in the org.hibernate.type.Type class hierarchy the

public boolean isEqual(Object x, Object y, EntityMode entityMode) method was failing.

What I did is I modified the Data Object and the hbm file like this

class Sample{

integer sampleId;

String sampleName;

public int getSampleId(){

public short getSampleId(){

….

}

public void setSampleId(int i){

public void setSampleId(short s){

…..

}

}

<id
name=”sampleId”
type=”integer”

type=”short”

column=”sample_id”
unsaved-value=”0″>
<generator class=”identity” />
</id>

May 16, 2007 - Posted by Paras | Hibernate | | 11 Comments

11 Comments »

  1. good job…keep it up…..

    Comment by Sudarshan Varma | July 14, 2007 | Reply

  2. Hi ,

    I am facing exactly the similar problem in teradata database .

    ‘identifier of an instance of com.liferay.portal.model.impl.GroupImpl was altered from 1001 to 1001 ‘ .

    My tables-hbm.xml file looks like this .

    I have all getters and setters in String only . The datatype for groupId is also varchar .

    Pls help .

    Thanks

    Comment by Chandrasekar | December 19, 2007 | Reply

  3. My tables-hbm.xml file looks like this .

    Sorry missed this part .

    tables-hbm.xml

    chandrasekar.T

    Comment by Chandrasekar | December 19, 2007 | Reply

  4. Thanks a lot. Mine was long in Java and int in the hbm file!

    Comment by Adarsh Ramamurthy | January 30, 2008 | Reply

  5. I faced the same problem and struggling for nearly a day. After looking at this site revisited my setters and getters and found I have return type as long for the getter and whereas the field was declared as int.

    Thanks for the post.

    Comment by Ravi Gopalan | March 2, 2008 | Reply

  6. Yes, i discovered something similar in my setters & getters causing same problem (i though).

    Thanks for post man

    Comment by Sd.Plox | June 25, 2008 | Reply

  7. Thank You for the post.

    Comment by Ian Bell | October 27, 2008 | Reply

  8. Thanks a lot. This works fine for me.

    Comment by linkerstorm | January 26, 2009 | Reply

  9. well described solution of problem

    Comment by sushnat | May 21, 2009 | Reply

  10. Hi all,
    there is a problem always using primitive types with Hibernate. So, the solution is using java.lang.Integer as data-type for IDs, for example.
    If you need more space to store the ID perhaps you’ll need to use the java.lang.Long … but … who’ll need such as space?!
    Regards.
    DonkanMcLeod

    Comment by donkanmcleod | June 18, 2009 | Reply

    • Yes, that’s correct. DonkanMcLeod. You should always use bigger datatypes in databases and as far as possible use hbm file generator tools to avoid “type-mismatch” errors.

      Comment by Paras | June 23, 2009 | Reply


Leave a comment