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.  [ 4 posts ] 
Author Message
 Post subject: How to delete items from the bag
PostPosted: Mon Aug 27, 2007 4:01 am 
Beginner
Beginner

Joined: Thu Oct 26, 2006 4:45 am
Posts: 39
Why is the following not deleting the bagged child objects?

Hibernate version: 1.0.4

Mapping documents:

[ Hotel.hbm.xml ]
<bag name="ImagesUntyped" table="HotelsImage" lazy="false" cascade="all">
-->
<key column="HotelId"/>
<one-to-many class="Airtrade.Business.Hotels.Image, Airtrade.Business.Hotels"/>

...

[ Image.hbm.xml ]

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" schema="dbo">
<class name="Airtrade.Business.Hotels.Image, Airtrade.Business.Hotels" table="HotelsImage">

<!-- business object id -->
<id name="Id" type="Int32" column="ImageId">
<generator class="identity" />
</id>

<many-to-one name="Hotel" class="Airtrade.Business.Hotels.Hotel, Airtrade.Business.Hotels" column="HotelId" />
.....

Name and version of the database you are using: MSSQL2000

The hotel.cs class is using ListWrapper like this:

public class Hotel
{
private IList untypedImages = new ArrayList();
public IList<Image> Images
{
get
{
return new ListWrapper<Image>(ImagesUntyped);
}
}

/// <summary>
/// Untyped version of the Images property for NHibernate
/// </summary>
public virtual IList ImagesUntyped
{
get
{
return untypedImages;
}
set
{
untypedImages = value;
}
}



The problem is when I assign a completely new list of Images to the hotel, the previous images that were loaded are not deleted. Why is that?

// get the hotel out of database
Hotel hotel = Session.Load(typeof(Hotel), hotelId);
// remove all previous images
hotel.ImagesUntyped.Clear();
// assign new images
foreach (Image hotelImage in newHotelImages)
{
hotelImage.Hotel = hotel; // bidirectional link
hotel.Images.Add(hotelImage);
}
Session.Update(hotel);

Now when I look in the database, the new images are only added to the hotel and the previous images where not deleted??


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 27, 2007 5:38 am 
Beginner
Beginner

Joined: Thu Oct 26, 2006 4:45 am
Posts: 39
Hmm.. I found out that if I explicitly delete all the child-objects, they get deleted but I thought NHibernate bag would take care of that?

Code:
foreach(Image image in cachedHotel.Images)
{
   Session.Delete(image);
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 04, 2007 7:51 am 
Beginner
Beginner

Joined: Thu Oct 26, 2006 4:45 am
Posts: 39
Anybody know why the child-objects are not deleted automatically?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 04, 2007 12:37 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Change
Code:
<bag name="ImagesUntyped" table="HotelsImage" lazy="false" cascade="all">
to
Code:
<bag name="ImagesUntyped" table="HotelsImage" lazy="false" cascade="all-delete-orphan" inverse="true">
.


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