-->
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.  [ 5 posts ] 
Author Message
 Post subject: Unable to delete child from collection
PostPosted: Fri Mar 23, 2007 9:57 am 
Beginner
Beginner

Joined: Fri Oct 06, 2006 7:11 am
Posts: 32
I have a problem deleting a child element from a collection.
I have cascade="all" and the SQL is correct but the entry is not deleted from the database. I've read up on similar problems and I don't seem to have anything wrong.

The CertType can contain 0-n StatementTitles but the StatementTitle can exist on its own. Therefore I only want to delete the reference to the StatementTitle i.e the entry in the DPCS_TYPE_STMT_TITLE_LINKS table and not the StatementTitle in the DPCS_STATEMENT_TITLE table.


The application has no problem adding and saving any child StatementTitles to the colleciton in the parent (CertType) class.

I've been pulling my hair out for 2 days on this and need some help.

thanks in advance


Hibernate version:
3.1

Mapping documents:
extract from CertType.hbm.xml that maps the collection

Code:
      <set name="certStatementTitles"
          table="DPCS_TYPE_STMT_TITLE_LINKS"
          cascade="all"
          order-by="STATEMENT_TITLE_ID asc"
          lazy="false">
          <key column="CERT_TYPE_ID"/>
          <many-to-many column="STATEMENT_TITLE_ID"
                     class="cert.StatementTitle"/>
      </set>


StatementTitle.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="cert.StatementTitle" lazy="false" table="DPCS_STATEMENT_TITLE">
       <id name="statementTitleId" column="STATEMENT_TITLE_ID" unsaved-value="0" type="long">
           <generator class="sequence">
              <param name="sequence">STATEMENT_TITLE_ID_SEQ</param>
           </generator>
      </id>
        <property name="title" column="STATEMENT_TITLE" type="string"/>
        <property name="selectable" column="SELECTABLE" type="string"/>
    </class>
</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():
Session session = HibernateUtil.getSession();
CertType certType = (CertType)session.get(CertType.class, certTypeId);

StatementTitle statementTitle = (StatementTitle)session.get(StatementTitle.class, statementTitleId);

certType.getCertStatementTitles().remove(statementTitle);
session.flush();


Name and version of the database you are using: Oracle 10i

The generated SQL (show_sql=true):
delete
from
dpcs.DPCS_TYPE_STMT_TITLE_LINKS
where
CERT_TYPE_ID=?
and STATEMENT_TITLE_ID=?


Problems with Session and transaction handling?

I'm using the
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.OC4JTransactionManagerLookup</property>


Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject: Transaction?
PostPosted: Fri Mar 23, 2007 10:41 am 
Regular
Regular

Joined: Wed Aug 24, 2005 11:49 am
Posts: 63
So the sql is what you expected right?
Try a beginTransaction and a commit.

Also, are you sure you want a many-to-many with cascade is all?
If you delete a 'CertType' all associated StatementTitles will be deleted (what if they are related to multiple CertTypes?)

_________________
Edwin van der Elst
Finalist IT Group


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 23, 2007 11:38 am 
Beginner
Beginner

Joined: Fri Oct 06, 2006 7:11 am
Posts: 32
Hi,

thanks for putting me on the right track. I'd forgot to start a transaction. Case of not seeing the wood for the trees.

As for the cascade='all', its only if you have the cascade set to "all,delete-orphan" that the child will be deleted or if you explicitly delete the child by calling session.delete(statementTitle);

thats fixed the problem
thanks


Top
 Profile  
 
 Post subject: Cascade ...
PostPosted: Fri Mar 23, 2007 11:53 am 
Regular
Regular

Joined: Wed Aug 24, 2005 11:49 am
Posts: 63
Cascade all will remove the childs.
It just means: if delete is called, call it on the elements in the collection as well.
Dangerous on many-to-many!


See http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#objectstate-transitive

_________________
Edwin van der Elst
Finalist IT Group


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 23, 2007 12:04 pm 
Beginner
Beginner

Joined: Fri Oct 06, 2006 7:11 am
Posts: 32
I know it would be very dangerous to delete the child entries from the StatementTitle table as there may be associations to other CertTypes, but Chapter 21 or the Hibernate Documentation mentions that only the 'link' between the tables is deleted. I have also tried this and the child StatementTitle is not deleted, only the entry in the joining table.

if you look at section 21.3. Cascading lifecycle on the link below

http://www.hibernate.org/hib_docs/v3/re ... child.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.