org.hibernate.HibernateException: identifier of an instance of members.Appointment was altered from 8 to 8

Full error message:

Exception occurred during event dispatching:
org.hibernate.HibernateException: identifier of an instance of members.Appointment was altered from 8 to 8
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:58)
at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:157)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:113)
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:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at members.AppointmentDialog.jButtonSaveActionPerformed(AppointmentDialog.java:279)
at members.AppointmentDialog.access$000(AppointmentDialog.java:21)
at members.AppointmentDialog$1.actionPerformed(AppointmentDialog.java:101)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:5517)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
at java.awt.Component.processEvent(Component.java:5282)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3984)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:153)
at java.awt.Dialog$1.run(Dialog.java:535)
at java.awt.Dialog$2.run(Dialog.java:563)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Dialog.show(Dialog.java:561)
at java.awt.Component.show(Component.java:1302)
at java.awt.Component.setVisible(Component.java:1255)
at admin.AdminOptionsForm.jButtonSetAppointmentActionPerformed(AdminOptionsForm.java:238)
at admin.AdminOptionsForm.access$800(AdminOptionsForm.java:24)
at admin.AdminOptionsForm$9.actionPerformed(AdminOptionsForm.java:145)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:5517)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
at java.awt.Component.processEvent(Component.java:5282)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3984)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

In Java, I had a class Appointment mapped to a MySQL table `appointment` using Hibernate as follows:

<hibernate-mapping>
<class name="members.Appointment" table="appointment">
<id name="idAppointment" column="idappointment" type="short">
<generator class="native"/>
</id>
<many-to-one name="member" column="idmember" not-null="true" foreign-key="fk_idmember"/>
<property name="startDate" type="timestamp" column="start_date"/>
<property name="duration" type="short" column="duration" length="3"/>
<property name="cancelled" type="boolean"/>
</class>
</hibernate-mapping>

I was getting the error when inserting a new appointment into the database.

The problem was that the idAppointment member variable in the Java class and the idappointment field in the MySQL database table were of the type int. So why the hell did I have the type short in the mapping? The answer is simple: because of the copy paste. So the correct code is:

<id name="idAppointment" column="idappointment">
<generator class="native"/>
</id>

Conclusion: copy paste is sometimes evil.

6 thoughts on “org.hibernate.HibernateException: identifier of an instance of members.Appointment was altered from 8 to 8

  1. I’m having the same error message. But the mistake I’ve made is different. I missed the getter method of the id property. As soon as I added the method, this error message don’t appear again.

    Thanks for the post. It gave me idea that the error message is concerned with the class mapped to the database.

  2. I am facing the same problem.. Did any body found the solution to this porblem?
    Please help.

  3. Hello!
    I’m having the same error to. My mistake was that my getter method of th id property was wrong:

    getHashCode wrong:
    public int getHashCode(){
    return this.hashCode();
    }

    getHashCode right:
    public int getHashCode(){
    return this.hashCode;
    }

    I wish it help you. Bye bye!!

    (Excuse if I write something wrong, my english isn’t right).

  4. I got the same error. My mistake is the datatype of the id property and it’s getter setter methods.

    private long id;
    public long getId() {
    return id;
    }
    public void setId(long l) {
    id = l;
    }

    I changed long to int and it worked.

  5. same error. I also changed the long datatype to int. That solved the problem.
    But can’t we set long datatype??

Leave a Reply

Your email address will not be published. Required fields are marked *