-->
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: Removing Items from an IDictionary doesn't result in delete
PostPosted: Thu Jul 12, 2007 4:05 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 3:07 pm
Posts: 77
The title is maybe a bit confusing, but my problem is as follows:

I have a class, which contains an IDictionary.
This IDictionary has a simple string as its key, and as 'value', it contains instances of another class.

In short, this class looks as this:
Code:
public class Class1
{
  private int _id;
  private IDictionary<string, Class2> _dictionary;
}


The mapping of this class, looks like this:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Domain.Class1, Domain" table="Table1" lazy="false">

    <id name="Id" access="field.camelcase-underscore" column="ProjectId" unsaved-value="-1">
      <generator class="identity" />
    </id>
   
    <map name="_class2Collection" access="field" table="Table2" lazy="false" cascade="all" inverse="true" >
      <key column="Id" />
      <index column="Language" type="string" />     
      <one-to-many class="Domain.Class2, Domain" />
    </map>
   
  </class>
</hibernate-mapping>


So, when I remove an item from the _dictionary member of class1, this does not result in a DELETE statment generated by NHibernate. NHibernate doesnt do anything.
Then, I thought about removing the 'inverse=true' attribute from the mapping; in this case, NHibernate generates an UPDATE statement: It wants to set the key (Id) and index (Language) column of Table2 to NULL (which is not allowed), instead of deleting the record from Table2.

(I must also say that my 'Class2' contains yet another dictionary wich just contains keys of type string and values of type string. When I remove an item from this dictionary, the items are deleted like they should be).

What am i missing ? What am i doing wrong ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 12, 2007 4:23 pm 
Beginner
Beginner

Joined: Tue Jul 10, 2007 5:27 am
Posts: 34
Location: Belgium
keep inverse="true" and change cascade to "all-delete-orphan"

_________________
Davy Brion
http://ralinx.wordpress.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 12, 2007 4:40 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 3:07 pm
Posts: 77
DavyBrion wrote:
keep inverse="true" and change cascade to "all-delete-orphan"

Thx for your quick response, however... this 'all-delete-orphan' is now a bit 'eager'...
It indeed deletes the item now, but it also deletes the record from the DB which represents my 'main' object (the instance of class1)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 12, 2007 5:06 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 3:07 pm
Posts: 77
Hmm, cascade='all-delete-orphan' indeed does the trick.
The problem I described earlier was caused because I called the SaveOrUpdate method of the Session; if I just use the Save method, it all works fine.


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.