-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: a different object with the same identifier value was al...
PostPosted: Mon May 10, 2010 9:47 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
I get the following error:
Quote:
ERROR - (EmployeesPanel.java:550) - a different object with the same identifier value was already associated with the session: [data.StoreEmployee#Auke-2010-130-11-40-44-233-0000]

StoreEmployee contains info about an employee and a store. See mapping below.
First I remove all associations and then I remove the object itself. I suspect the object can get deleted automatically, because there are no more references, but when I don't it isn't deleted automatically.

This is the SessionHandler I use:
Code:
import org.hibernate.*;
import org.hibernate.cfg.*;
import data.Data;

public class SessionHandler
{
   private static SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
   
   public static Session getSession()
   {
      return sessionFactory.openSession();
   }
   
   public static void closeSession(Session session)
   {
      session.flush();
      session.close();
   }
   
   public static void save(Session session, Data data)
   {
      Logger.get().info("save," + data.toString());
      session.save(data);
   }
   
   public static void update(Session session, Data data)
   {
      Logger.get().info("update," + data.toString());
      session.update(data);
   }
   
   public static void delete(Session session, Data data)
   {
      Logger.get().info("delete," + data.toString());
      session.delete(data);
   }
}

Here is the code where the error appears:
Code:
Session session = SessionHandler.getSession();
Transaction transaction = session.beginTransaction();
try
{
   Employee employee = Employee.getEmployeeById(session, selectedEmployee.getId());

   StoreEmployee storeEmployee = employee.getStoreEmployee();
   employee.getStoreEmployees().remove(storeEmployee);
   SessionHandler.update(session, employee);
               
   Store store = Store.getStoreById(session, SystemConfig.STORE_ID);
   store.getStoreEmployees().remove(storeEmployee);
   SessionHandler.update(session, store);
               
   Position position = storeEmployee.getPosition();
   if (position != null)
   {
      position = Position.getPositionById(session, position.getId());
      position.getStoreEmployees().remove(storeEmployee);
      SessionHandler.update(session, position);
   }
   SessionHandler.delete(session, storeEmployee);               // This line throws the exception
   transaction.commit();
}
catch (Exception e)
{
   transaction.rollback();
   Logger.get().error(e.getMessage());                        // Line 550
}
SessionHandler.closeSession(session);

Mapping:
Code:
   <class name="data.StoreEmployee" table="storeEmployees">
      <id name="id" column="id" length="31">
         <generator class="assigned" />
      </id>

      <properties name="storeEmployeeUnique" unique="true">
         <many-to-one name="store" class="data.Store" column="store" not-null="true" />
         <many-to-one name="employee" class="data.Employee" column="employee" not-null="true" />
      </properties>
      <many-to-one name="position" class="data.Position" column="position" lazy="false" />
      ...
   </class>

I suspect it's something with the sessions, or I'm just doing something wrong to delete this object.

Note:
The sessionHandler and the mapping have worked for along time.
The difference is: so far I deleted one object of a relation and cascade would delete the relation. Now I need to delete the relation without deleting the objects related to.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.