• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic