-->
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.  [ 3 posts ] 
Author Message
 Post subject: Urgent! Delete on One-To-Many
PostPosted: Wed Nov 26, 2003 10:30 pm 
Newbie

Joined: Sun Nov 23, 2003 4:46 am
Posts: 6
Hi guys,

I'm facing a problem while trying to delete a one-to-many records.

Mapping as below
-----------------------------------------------------------------------------------------
<class name="berth.hibernate.entity.TrxVisit" table="trx_visit">
<id name="visitId" column="visit_id" type="java.lang.String" unsaved-value="0">
<generator class="assigned"/>
</id>
<property name="visitType" column="visit_type" type="java.lang.String" length="2" not-null="true"/>
<property name="visitCarrier" column="visit_carrier" type="java.lang.String" length="255" not-null="true"/>
<set name="cranes" table="trx_crane" cascade="all-delete-orphan">
<key column="cvisit_id"/>
<one-to-many class="berth.hibernate.entity.TrxCrane"/>
</set>
</class>
---------------------------------------
<class name="berth.hibernate.entity.TrxCrane" table="trx_crane">
<composite-id>
<key-property name="craneId" column="crane_id" type="java.lang.String" length="5"/>
<key-property name="cvisitId" column="cvisit_id" type="java.lang.String" length="10"/>
</composite-id>
<property name="craneMph" column="crane_mph" type="java.lang.Integer" length="3" not-null="true"/>
</class>
-----------------------------------------------------------------------------------------
CREATE TABLE `trx_visit` (
`visit_id` varchar(10) NOT NULL default '0',
`visit_type` char(2) NOT NULL default '0',
`visit_carrier` varchar(255) NOT NULL default '0',
PRIMARY KEY (`visit_id`),
UNIQUE KEY `visit_id` (`visit_id`)
);

CREATE TABLE `trx_crane` (
`crane_id` varchar(5) NOT NULL default '0',
`cvisit_id` varchar(10) NOT NULL default '0',
`crane_mph` int(3) unsigned NOT NULL default '0',
PRIMARY KEY (`crane_id`,`cvisit_id`),
);

TrxVisit 1 - N TrxCrane
TrxCrane contains a composite id where one of the key is the primart for TrxVisit.

When I perform the below codes
TrxVisit visit = (TrxVisit) sess.load(TrxVisit.class, "4");
sess.delete(visit);

10:10:33,170 INFO [STDOUT] Hibernate: select trxvisit0_.visit_id as visit_id, trxvisit0_.visit_type as visit_type, trxvisit0_.visit_carrier as visit_ca3_ from trx_visit trxvisit0_ where trxvisit0_.visit_id=?
10:10:33,186 INFO [STDOUT] Hibernate: select trx_cran0_.crane_id as crane_id__, trx_cran0_.cvisit_id as cvisit_id__, trx_cran0_.crane_id as crane_id, trx_cran0_.cvisit_id as cvisit_id, trx_cran0_.crane_mph as crane_mph from trx_crane trx_cran0_ where trx_cran0_.cvisit_id=?
10:10:33,248 INFO [STDOUT] Hibernate: update trx_crane set cvisit_id=null where cvisit_id=?
10:10:33,264 INFO [STDOUT] Hibernate: delete from trx_crane where crane_id=? and cvisit_id=?
10:10:33,264 INFO [STDOUT] Hibernate: delete from trx_visit where visit_id=?

It perform 5 statements
-The statement 3 which assign the cvisit_id to null, why it happened?
If the cvisit_id has been assign to null, it cant be deleted on the statement 4

Can someone please help me?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 10:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Urgent! Please read the documentation.

(Parent/Child Relationship)


Top
 Profile  
 
 Post subject: Gotcha
PostPosted: Wed Nov 26, 2003 11:20 pm 
Newbie

Joined: Sun Nov 23, 2003 4:46 am
Posts: 6
Thanks.

I added the inverse="true", it works
<set name="cranes" table="trx_crane" inverse="true">
<key column="cvisit_id"/>
<one-to-many class="berth.hibernate.entity.TrxCrane"/>
</set>

The above will perform
- [STDOUT] Hibernate: delete from trx_visit where visit_id=?


If I added inverse="true" and cascade="all-delete-orphan">
<set name="cranes" table="trx_crane" inverse="true" cascade="all-delete-orphan">
<key column="cvisit_id"/>
<one-to-many class="berth.hibernate.entity.TrxCrane"/>
</set>
- [STDOUT] Hibernate: delete from trx_crane where crane_id=? and cvisit_id=?
- [STDOUT] Hibernate: delete from trx_visit where visit_id=?

From my understanding,
If I add a foreign key constraint on the trx_crane table on cascade=delete
I only need to add inverse="true"

If I didnt add a cascade=delete on the trx_crane,
I need to add inverse="true", and cascade="all-delete-orphan"

Should I leave the works to hibernate or should I do the work on the database side?

Or should I do both
add the cascade=delete on child table
and added in inverse="true" , and cascade="all-delete-orphan"?


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