-->
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: Collections of dependent objects - cascading delete?
PostPosted: Tue Feb 14, 2006 3:32 am 
Newbie

Joined: Sun Nov 07, 2004 3:38 am
Posts: 3
Hibernate version: 3.1

I read section 8.2 of the tutorial, about mapping collections of dependent objects. In that example, a set contains Name objects that are mapped as composite elements. My question is, how can I cause these composite elements to delete when the containing element is removed? Currently, I cannot remove the containing element, because attampting to removing it causes a constraint violation. Here is the mapping document. Basically, Person has a set of Addresses, and I want to map Address as a component of Person:

Code:
<class name="Person" table="person" lazy="false">
        <id name="id" type="long" column="id">
            <generator class="native"/>
        </id>

        <set name="addresses" table="person_addresses">
             <key column="id"/>
            <composite-element class="Address">
                 <property name="street" column="street" type="string"/>
                 <property name="streetLineTwo" column="streetLineTwo" type="string"/>
                 <property name="city" column="city" type="string"/>
                 <property name="state" column="state" type="string"/>
                 <property name="zip" column="zip" type="string"/>
             </composite-element>
         </set>



Address elements are correctly saved, but, again, I can't remove a Person if that person has addresses.

Thanks.


Top
 Profile  
 
 Post subject: cascade
PostPosted: Thu Feb 16, 2006 6:06 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
<set name="addresses" table="person_addresses" cascade="all>

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: Re: cascade
PostPosted: Fri Apr 14, 2006 2:09 am 
Newbie

Joined: Fri Apr 14, 2006 1:50 am
Posts: 8
Location: Peking
kgignatyev wrote:
<set name="addresses" table="person_addresses" cascade="all>


this doesn't work in my situation. I'm using oracle 10g along with hibernate 3.1.2

here is the mapping file:

<?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="events.Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="native"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
<set name="eas" table="EVENT_ATTR" cascade="all">
<key column="EVENT_ID"/>
<composite-element class="events.EventAttr">
<property name="attr1" type="string" length="40" column="ATTR_1"/>
<property name="attr2" type="string" length="40" column="ATTR_2"/>
<property name="attr3" type="string" length="40" column="ATTR_3"/>
</composite-element>
</set>
</class>
</hibernate-mapping>

And I got this message when i delete Event object:

Caused by: java.sql.SQLException: ORA-02292: 违反完整约束条件 (TOR.FK59508C96F96D1A45) - 已找到子记录

you may not read chinese characters but you can see 'ORA-02292'

_________________
bravery can set you free.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 14, 2006 12:21 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Hibernate does take care of relationships just fine.
I suspect that something in your database references some rows you are trying to delete.

Please try to delete the same rows you area trying to delete in H from event_attr and avents table with using SQL in the sqlplus and see if you will get the same exception.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 15, 2006 2:32 am 
Newbie

Joined: Fri Apr 14, 2006 1:50 am
Posts: 8
Location: Peking
kgignatyev wrote:
Hibernate does take care of relationships just fine.
I suspect that something in your database references some rows you are trying to delete.

Please try to delete the same rows you area trying to delete in H from event_attr and avents table with using SQL in the sqlplus and see if you will get the same exception.


YES! I got the same exception. I checked out the table hibernate generated, the attribute table(EVENT_ATTR)'s foreign key is not set as "ON DELETE CASCADE" in DDL. But, when deleting the event object, hibernate just issue "delete from event" without first issuing "delete from event_attr where ...". I'm wondering how to configure composite-element to let hibernate enabling "ON DELETE CASCADE" in the attribute table.

_________________
bravery can set you free.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 15, 2006 4:45 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
IMO it is not good idea to let Hibernate create database schema for you. It is beter to create DB schema from a DDL script exactly as it needs to be and let H take care of mapping only.

See relevant discussions:
http://forum.hibernate.org/viewtopic.ph ... ght=schema
http://forum.hibernate.org/viewtopic.ph ... ight=erwin

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 15, 2006 9:54 pm 
Newbie

Joined: Fri Apr 14, 2006 1:50 am
Posts: 8
Location: Peking
kgignatyev wrote:
IMO it is not good idea to let Hibernate create database schema for you. It is beter to create DB schema from a DDL script exactly as it needs to be and let H take care of mapping only.

See relevant discussions:
http://forum.hibernate.org/viewtopic.ph ... ght=schema
http://forum.hibernate.org/viewtopic.ph ... ight=erwin


Oh, thanks. I'd better adopt your suggestion. In that way, I was enabled with control of many DBMS-specific features.

What I found last night is, I indeed deleted the Event object using Session.delete(). But If I deleted with HQL:"delete Event" in the same situation, it failed. Anyway, it is all about the "CASCADE DELETE" feature of foreign key. Thought this can be solved by creating the schema manually, I think Hibernate should check for HQL the "cascade delete" flag of a collection attribute instead of, i guess, directely translate HQL to SQL.

_________________
bravery can set you free.


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.