Home  >  Forum  >  Hibernate
Post New Query

different object with the same identifier value was already associated


joined on
March 18,2013
Asked on May 19,2013
Hi Friends,
Any clue for below error.

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:191) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:144) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:693) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:689)
joined on
March 14,2013
Replied on May 19,2013

Can you explain what are doing.
joined on
March 18,2013
Replied on May 19,2013

I am running InheritanceType.TABLE_PER_CLASS and saving data like

            Session session=getSession();
            session.beginTransaction();
            Employee e=new Employee(1,"james",25);
            session.save(e);
            Manager m=new Manager(1,"james",25,"SC","ABC");
            session.save(m);
            session.beginTransaction();
       
            session.getTransaction().commit();

joined on
March 14,2013
Replied on May 19,2013

You need to use as
session.flush();
session.clear();

your code should look like
Session session=getSession();
session.beginTransaction();
Employee e=new Employee(1,"james",25);
session.save(e);
Manager m=new Manager(1,"james",25,"SC","ABC");
session.flush();
session.clear();
session.save(m);
          session.beginTransaction();
session.getTransaction().commit();





Write Answer








Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us