-->
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.  [ 7 posts ] 
Author Message
 Post subject: help me with all-delete-orphan
PostPosted: Wed Oct 05, 2005 4:15 am 
Beginner
Beginner

Joined: Tue Nov 11, 2003 4:49 am
Posts: 47
Location: Florence, Italy
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:

_________________
Ciao.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 4:28 am 
Newbie

Joined: Wed Oct 05, 2005 3:36 am
Posts: 15
I am very new to hibernate myself, but have you tried using the <set> instead of <bag> in your configuration file.

I think that might be whats causing the extra database operations.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 4:59 am 
Beginner
Beginner

Joined: Tue Nov 11, 2003 4:49 am
Posts: 47
Location: Florence, Italy
Thank you for your help, but the problem is not related to the number of Database interaction, but to the fact that I found persisted an entity that I removed from an "all-delete-orphan" collection.

_________________
Ciao.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 9:43 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 4:27 pm
Posts: 40
Location: canada
you need to explicitly delete the step object. all-delete-orphan means that when the parent object (entry) is deleted, that all the step objects it contains in its collection will also be deleted.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 10:03 am 
Beginner
Beginner

Joined: Tue Nov 11, 2003 4:49 am
Posts: 47
Location: Florence, Italy
From
Quote:
Hibernate in Action
:

"■ cascade="all" means to cascade both save-update and delete, as well as
calls to evict and lock.
■ cascade="all-delete-orphan" means the same as cascade="all" but, in addition,
Hibernate deletes any persistent entity instance that has been removed
(dereferenced) from the association
(for example, from a collection)."

so it's not true what you said !!!!!
And also I thougth that Hibernate would delete that entity for me !!!!
It's so a simple mapping that surely I am missing something under my eyes.....

_________________
Ciao.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 10:25 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 4:27 pm
Posts: 40
Location: canada
how about calling session.update(entry) before you close the session?

Code:
entry.getCurrentSteps().remove(step);
session.update(entry);
session.close();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 10:31 am 
Beginner
Beginner

Joined: Tue Nov 11, 2003 4:49 am
Posts: 47
Location: Florence, Italy
Thank you for your help, but my question is more philosophical than pratical.
If I put a session.delete(step) everything works as it should, but I want Hiberante to do the housework for me.

_________________
Ciao.


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

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.