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
|