I mapped a collection between two entities with all-delete-orphan and I expected that, when I remove an element from that collection Hibernate, on flush, would not persist that entity, or at least insert and delete it.
Instead as you can see below not only the object is persisted but is also updated !!!!
Of course there is something strange, I have to save the object (also with the cascade="all") cause the system need immediately the generated "id".
Also, the problem is now solved with an explicit delete of the object.
Let me know where I misunderstood Hibernate.
Thank you.
Hibernate version: 2.1.8
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.opensymphony.workflow.spi.hibernate.HibernateWorkflowEntry" table="OS_WFENTRY">
<id name="id" unsaved-value="-1">
<generator class="native"/>
</id>
<version name="version"/>
<property name="workflowName" column="name"/>
<property name="state"/>
<bag name="currentSteps" cascade="all-delete-orphan" lazy="true" inverse="true">
<key column="entry_Id"/>
<!-- index column="stepIndex"/-->
<one-to-many class="com.opensymphony.workflow.spi.hibernate.HibernateCurrentStep"/>
</bag>
<bag name="historySteps" cascade="all" lazy="true" inverse="true">
<key column="entry_Id"/>
<!-- index column="stepIndex"/-->
<one-to-many class="com.opensymphony.workflow.spi.hibernate.HibernateHistoryStep"/>
</bag>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close(): The thing that is now working as I thougth is done in many classes, but I have replicated it with this simple code runned into a JUnit test:
session = sessionFactory.openSession();
HibernateWorkflowEntry entry = new HibernateWorkflowEntry();
session.save(entry);
HibernateCurrentStep step = new HibernateCurrentStep();
step.setEntry(entry);
entry.getCurrentSteps().add(step);
step.setActionId(10);
entry.getCurrentSteps().remove(step);
session.close();
Full stack trace of any exception that occurs: No Exception
Name and version of the database you are using: Oracle 9.1.2
The generated SQL (show_sql=true): Code:
05-10-2005 10:07:53 [main] DEBUG net.sf.hibernate.SQL - select hibernate_sequence.nextval from dual
05-10-2005 10:07:54 [main] DEBUG net.sf.hibernate.SQL - select hibernate_sequence.nextval from dual
05-10-2005 10:07:54 [main] DEBUG net.sf.hibernate.SQL - select hibernate_sequence.nextval from dual
05-10-2005 10:07:54 [main] DEBUG net.sf.hibernate.SQL - insert into OS_WFENTRY (version, name, state, id) values (?, ?, ?, ?)
05-10-2005 10:07:54 [main] DEBUG net.sf.hibernate.SQL - insert into OS_CURRENTSTEP (action_Id, caller, finish_Date, start_Date, due_Date, owner, status, step_Id, entry_Id, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
05-10-2005 10:07:54 [main] DEBUG net.sf.hibernate.SQL - insert into OS_HISTORYSTEP (action_Id, caller, finish_Date, start_Date, due_Date, owner, status, step_Id, entry_Id, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
05-10-2005 10:07:54 [main] DEBUG net.sf.hibernate.SQL - update OS_CURRENTSTEP set action_Id=?, caller=?, finish_Date=?, start_Date=?, due_Date=?, owner=?, status=?, step_Id=?, entry_Id=? where id=?
05-10-2005 10:07:54 [main] DEBUG net.sf.hibernate.SQL - select hibernatew0_.id as id0_, hibernatew0_.version as version0_, hibernatew0_.name as name0_, hibernatew0_.state as state0_ from OS_WFENTRY hibernatew0_ where hibernatew0_.id=?
05-10-2005 10:07:54 [main] DEBUG net.sf.hibernate.SQL - select currentste0_.entry_Id as entry_Id__, currentste0_.id as id__, currentste0_.id as id0_, currentste0_.action_Id as action_Id0_, currentste0_.caller as caller0_, currentste0_.finish_Date as finish_D4_0_, currentste0_.start_Date as start_Date0_, currentste0_.due_Date as due_Date0_, currentste0_.owner as owner0_, currentste0_.status as status0_, currentste0_.step_Id as step_Id0_, currentste0_.entry_Id as entry_Id0_ from OS_CURRENTSTEP currentste0_ where currentste0_.entry_Id=?
Debug level Hibernate log excerpt: