Note that there are some explanatory texts on larger screens.

plurals
  1. POFluent NHibernate exception moving objects between collections
    text
    copied!<p>When moving an object from one collection to another and when cascade is set to all-delete-orphan, I get the following exception:</p> <p>deleted object would be re-saved by cascade (remove deleted object from associations)</p> <p>I thought that nhibernate would not delete an object when it is referenced in another collection when you use all-delete-orphan.</p> <p>Can anyone confirm that, when you have objects like Folders which contain Folders or Files and you move a File from one Folder to another, you should not get this exception?</p> <p>I made a sample project in vs2010 which demonstrates this behavior. Can anyone say if my mappings are correct or if there is a bug in nhibernate?</p> <p>FileMapping.cs</p> <pre><code>public class FileMapping: ClassMap&lt;File&gt; { public FileMapping() { Id(x =&gt; x.Id, "Id").GeneratedBy.Native("File_seq"); Map(x =&gt; x.Name, "Name").Not.Nullable(); References(x =&gt; x.Folder).Not.Nullable().Column("idFolder"); } } </code></pre> <p>FolderMapping.cs</p> <pre><code>public class FolderMapping: ClassMap&lt;Folder&gt; { public FolderMapping() { Id(x =&gt; x.Id, "Id").GeneratedBy.Native("Folder_seq"); Map(x =&gt; x.Name, "Name").Not.Nullable(); HasMany(x =&gt; x.Folders).Inverse().Cascade.AllDeleteOrphan().KeyColumn("idParentFolder"); HasMany(x =&gt; x.Files).Inverse().Cascade.AllDeleteOrphan().KeyColumn("idFolder"); References(x =&gt; x.ParentFolder).Nullable().Column("idParentFolder"); } } </code></pre> <p>Sample project: <a href="http://www.mediafire.com/?orxcw63aziq54xo" rel="nofollow" title="vs2010 sample project">http://www.mediafire.com/?orxcw63aziq54xo</a> Instructions:</p> <ol> <li>make sure connectionstring in Project's Properties is correct</li> <li>run project</li> <li>click 1st button: connect to database</li> <li>click top right button to create tables and sample data (2 folder objects and 1 file)</li> <li>click button to move file object to other folder object</li> <li>click button to persist chances: you will get the DeletedObjectException</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload