Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

org.hibernate.HibernateException: identifier of an instance of Address was altered from 2 to 3

 
sandhya gokhale
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 given below:
there is no cascade here, so i am not updating the parent , but updating reference in child.

In the session I do
start transaction
session.get(StudentClass, id);
and update the AddressID from 2 to 3
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?
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not have to change an ID by yourself. The hibernate will do it.

You can use the cascade on your mapping (@OneTo....).

If you have an relationship bidirectional you will need to point eachother like

a.setB(b);

b.setA(a);

I wrote an post about it. I hope it might help you:JPA @OneToOne Unidirectional and Bidirectional
 
sandhya gokhale
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me give more specifics:

I read the Student object and update the referred parent.
When I use unique key from the referred/parent object I face no problems.

When my association refers to primary key and not unique key of parent then I face no problems
Problem faced when

Relationship mapped as

<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>

NO PROBLEM when:

<many-to-one name="StudentAddress" property-ref="UK_Address" name="com.san.Address" fetch="join" unique="true" update="true" insert="true" optimistic-lock="true" not-found="exception" embed-xml="true">
<column name="pinCode" not-null="true"/>
<column name="country" not-null="true"/>
</many-to-one>



my parent class is:
<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>
<properties name="UK_Address" unique="true" update="true" insert="true" optimistic-lock="true">
<property name="PinCode" type="java.lang.Long" column="PinCode" unique="false" optimistic-lock="true" lazy="false" generated="never"/>
<property name="Country" type="java.lang.String" column="Country" unique="false" optimistic-lock="true" lazy="false" generated="never"/>
</properties>

</class>



Caused by:
org.hibernate.HibernateException: identifier of an instance of Address was a
ltered from 3 to 2
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(Defau
ltFlushEntityEventListener.java:58)
at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(Def
aultFlushEntityEventListener.java:164)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity
(DefaultFlushEntityEventListener.java:120)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(A
bstractFlushingEventListener.java:196)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverything
ToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlus
hEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
 
sandhya gokhale
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This issue was resolved , by creating a new object of Address with the given id and replacing the address object associated with object Student.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic