Hi,
I am using NHibernate over a SQL Server database view rather than working directly with the underlying table. The view simply excludes objects that have their IsDeleted property set to true.
My problem arises when I update a parent object where one of the changes is that a child's IsDeleted property is now True.
It appears that NHibernate is checking whether the row was updated by looking at the affected row count but the row has been hidden by the view and so the row count comes back as 0.
Does anyuone have any suggestions on how I can get NHibernate to work over the view?
Interestingly enough, if the parent object is changed so that its IsDeleted property becomes True, then NH doesn't throw an exception.
BTW, having the view is non-negotiable.
Thanks,
Dean.
NHibernate version:
1.0.1
Code between sessionFactory.openSession() and session.close():
ISession hibernateSession = CreateHibernateSession();
IList outArray = new ArrayList();
ITransaction t = null;
try
{
t = hibernateSession.BeginTransaction();
foreach (IDomainObject domObj in objectList)
{
hibernateSession.SaveOrUpdate(domObj);
}
t.Commit();
}
Full stack trace of any exception that occurs:
NHibernate.Impl.SessionImpl [(null)] - could not synchronize database state with session
NHibernate.HibernateException: SQL insert, update or delete failed (expected affected row count: 1, actual affected row count: 0). Possible causes: the row was modified or deleted by another user, or a trigger is reporting misleading row count.
at NHibernate.Impl.NonBatchingBatcher.AddToBatch(Int32 expectedRowCount)
at NHibernate.Collection.AbstractCollectionPersister.Recreate(PersistentCollection collection, Object id, ISessionImplementor session)
at NHibernate.Impl.ScheduledCollectionRecreate.Execute()
at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
at NHibernate.Impl.SessionImpl.ExecuteAll(IList list)
at NHibernate.Impl.SessionImpl.Execute()
Name and version of the database you are using:
SQLServer 2000
The generated SQL (show_sql=true):
not available
Debug level Hibernate log excerpt:
Mapping documents:
NHibernate.HibernateException: SQL insert, update or delete failed (expected affected row count: 1, actual affected row count: 0). Possible causes: the row was modified or deleted by another user, or a trigger is reporting misleading row count.
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="EAF.Domain.Model.Definition, EAF.Domain.Model" table="Definition" discriminator-value="Definition">
<meta attribute="scope-class" inherit="false">public abstract</meta>
<id name="Id" type="EAF.Persistence.NullGuidUserType, EAF.Persistence" column="Id">
<generator class="assigned" />
</id>
<discriminator column="DEFINITION_TYPE" type="string" />
<version name="Version" column="Version" type="Int32" unsaved-value="-1" />
<property name="_name" type="String(255)" column="Name" access="field" />
<property name="_pluralName" type="String(2048)" column="PluralName" access="field" />
<property name="Description" type="String(2048)" column="Description" />
<property name="IsDeleted" type="YesNo" column="IsDeleted" />
<list name="Components" inverse="false" lazy="false" cascade="save-update">
<key column="ComponentsId" />
<index column="ListIndex" />
<one-to-many class="EAF.Domain.Model.Definition,EAF.Domain.Model" />
</list>
</class>
</hibernate-mapping>