-->
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.  [ 2 posts ] 
Author Message
 Post subject: Deleting childs automatically
PostPosted: Fri Dec 30, 2005 7:11 am 
Newbie

Joined: Fri Dec 30, 2005 7:03 am
Posts: 1
Hello friends,

I've a question about a parent/child relation.

I've a parent and I've mapped the childs with a Set and with the property cascade="all", then I suppose that when I delete the parent then the childs must be deleted automatically because the cascade="all" property, don't they ?

My problem is that if I create a foreign key in the child table this work, but if I don't create the foreign key this don't work. I suposse that independently if I create or not the foreign key, Hibernate should throw a DELETE sentence to delete the parent and another DELETE sentence to delete the childs, doesn't it ?

Sorry for my ignorance.

I attach my table descriptors and my hibernate mappings to see if you could clarify my doubts.
Thank you very much for your help



************** TABLES ************************


CREATE TABLE albums (
id bigint(20) unsigned NOT NULL auto_increment,
name varchar(45) NOT NULL default '',
description varchar(200) default NULL,
PRIMARY KEY (id)
) TYPE=InnoDB;



CREATE TABLE pictures (
id bigint(20) unsigned NOT NULL auto_increment,
title varchar(45) NOT NULL default '',
description varchar(200) default NULL,
album_id bigint(20) unsigned NOT NULL default '0',
PRIMARY KEY (id),
KEY Index_2 (album_id),
CONSTRAINT `FK_picture_1` FOREIGN KEY (`album_id`) REFERENCES `albums` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) TYPE=InnoDB;


************** MAPPINGS ************************


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
SYSTEM
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="gallery.model">

<class table="albums" name="Album">

<id name="id" column="id">
<generator class="increment"/>
</id>

<property name="name" type="string" column="name"/>
<property name="description" type="string" column="description"/>

<set name="pictures"
lazy="false"
inverse="true"
cascade="all">

<key column="album_id" on-delete="cascade"/>
<one-to-many class="Picture"/>

</set>

</class>

<query name="getListAlbums">
from Album as album
</query>


</hibernate-mapping>




<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
SYSTEM
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="gallery.model">

<class table="pictures" name="Picture">

<id name="id" column="id">
<generator class="increment"/>
</id>

<property name="title" type="string" column="title"/>
<property name="description" type="string" column="description"/>

<many-to-one name="album"
column="album_id"
class="Album"/>


</class>

<query name="getListPictures">
from Picture as picture
</query>



</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 30, 2005 11:12 am 
Beginner
Beginner

Joined: Sat Dec 17, 2005 1:24 pm
Posts: 42
Location: Berlin, Germany
Did you check that thread...

http://forum.hibernate.org/viewtopic.php?t=950912

Does that match your problem?

All the best,

René


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