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>
|