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?