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.