views:

594

answers:

1

I need some help figuring out how to proceed. I'm building an app to manage a record collection and i have the following mappings.

<class name="Soulful.Core.Domain.Model.Record,Soulful.Core" table="Record">
    <id name="Id" type="Int32" unsaved-value="0">
        <generator class="native" />
    </id>

    <many-to-one name="Artist" class="Soulful.Core.Domain.Model.Artist,Soulful.Core" foreign-key="FK_Artist_Id" cascade="all">
        <column name="ArtistId" not-null="true" />
    </many-to-one>

    .... more properties
</class>

<class name="Soulful.Core.Domain.Model.Artist,Soulful.Core" table="Artist">
    <id name="Id" type="Int32" unsaved-value="0">
        <generator class="native" />
    </id>

    <property name="Name" type="string" not-null="true" />
</class>

I want many records to map to one artist and this works just fine. But I run into trouble when I'm trying to delete a record. What I would like to do is:

Delete the record and the artist if no other records are connected to that artist. If there are records connected to the artist, just delete the record.

With the current mapping I get the following error when I call session.Delete(record);:

NHibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations)[Soulful.Core.Domain.Model.Artist#3]

Here's what my DeleteMethod looks like:

public virtual void Delete(T entity)
{
    using (var session = GetSession())
    using (var transaction = session.BeginTransaction())
    {
        try
        {
            session.Delete(entity);
            transaction.Commit();
        }
        catch (HibernateException)
        {
            transaction.Rollback();
            throw;
        }
    }
}

What would I need to do to have it working the way I want it to? Please feel free to state obvious.

Update I guess I'm asking if I need to handle deletion of artists manually?

A: 

Well, I don't think the Record should have a cascade="all" on the Artist.

The problem is that you call delete on the Artist and when you save the Record it will try to re-save the artist (because of the cascade). Hence the error :).

Since you have not-null on the Record's Artist property , you shouldn't delete an Artist, but delete the Record and the Artist should be deleted automatically .

sirrocco
Thanks for your reply! Well, I am calling Delete on the Record entity and not on the Artist entity. When cascade is set to all it will accurately delete the record and the artist but it will fail if the artist is connected to any other records. In that case I would like it to just delete the record. I tried setting cascade to "save-update" but then the artist is never deleted. Is there no way to set it up so that it can handle both scenarios?
Kristoffer Ahl
Well, if Artist is referenced by multiple Records, and you delete the Artist - wouldn't you end up with inconsistency in the Database ? - with Records that have non existent Artists ? Don't you have FKs in the DB ?
sirrocco
Yes, that would result in records without artists. And that is not what I want. I guess I'm asking if I need to handle deletion of artists manually? – Kristoffer Ahl 14 hours ago [delete this comment]
Kristoffer Ahl