-->
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 association by PK instead of FK
PostPosted: Sun Nov 27, 2005 5:49 am 
Newbie

Joined: Sun Nov 27, 2005 5:36 am
Posts: 2
I have a unidirectional one-to-many relation between hotel and hotelroom (one hotel has several rooms). When i load a hotel with say 4 rooms and then delete it, then hibernate issues 5 delete statements (one for hotel, 4 for the rooms) since the rooms are not deleted by using the FK to the hotel but the PK of the room (see statements below).
i expected something like:
Hibernate: delete from T_HOTELROOM where HOTEL_REF=?
Hibernate: delete from T_HOTEL where HOTEL_ID=?

How can i achieve this ?

thanks for your help and have a nice weekend.

fab

Hibernate version:
3.0.5

Mapping documents:
Mapping of the many rooms in one Hotel
<set
name="rooms" cascade="all" inverse="true">
<key column="HOTEL_REF" not-null="false"/>
<one-to-many class="HotelRoom"/>
</set>

Code between sessionFactory.openSession() and session.close():
Object obj = session.get(Hotel.class, new Long(12));
session.delete(obj);

Name and version of the database you are using:
Mysql 5

The generated SQL (show_sql=true):
Hibernate: delete from T_HOTELROOM where HOTELROOM_ID=?
Hibernate: delete from T_HOTELROOM where HOTELROOM_ID=?
Hibernate: delete from T_HOTELROOM where HOTELROOM_ID=?
Hibernate: delete from T_HOTELROOM where HOTELROOM_ID=?
Hibernate: delete from T_HOTEL where HOTEL_ID=?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 27, 2005 6:38 am 
Newbie

Joined: Sun Nov 27, 2005 5:36 am
Posts: 2
after doing some more experiments i can partialy answer this question myself:
by adding an on-delete="cascade" to the sets key element and changing the cascade to "save-update" the hotel gets deleted by using one statement since the DB will take care of the cascading.

Quote:
<set
name="rooms" cascade="save-update" inverse="true">
<key column="HOTEL_REF" not-null="false" on-delete="cascade"/>
<one-to-many class="Room"/>
</set>


still i would like to know if hibernate can issue the delete based on the FK on the hotel instead of the PK of the rooms ?


fab


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.