I'm attempting a many to many relationship with stored procedures. We're required that all table access go through sprocs. If I add a relationship between an "Employee" and a "Store" the query specified by <sql-insert> is invoked, but when attempting to remove the relationship dynamic sql is generated and the <sql-delete> statement (exec Test.dbo.DeleteStoreEmployeeByEmployee) is never invoked.
Thanks.
Hibernate version:
NHibernate 1.2.0.CR1
Mapping documents:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="Test.dbo" assembly="EmployeeSample" namespace="CSI.Samples.NHibernate">
<class name="Employee" table="Employee" optimistic-lock="version">
<id name="EmployeeId" column="EmployeeId" type="Guid">
<generator class="guid" />
</id>
<version column="Version" type="Int32" name="Version" unsaved-value="0" />
<bag name="Stores" lazy="true" generic="true" table="StoreEmployee">
<key column="EmployeeId" />
<many-to-many class="Store" column="StoreId" />
<loader query-ref="SelectStoreByEmployeeId" />
<sql-insert>exec Test.dbo.InsertStoreEmployee ?, ?</sql-insert>
<sql-delete>exec Test.dbo.DeleteStoreEmployeeByEmployee ?</sql-delete>
</bag>
<property column="FirstName" type="String" name="FirstName" />
<property column="LastName" type="String" name="LastName" />
<loader query-ref="SelectEmployee"/>
<sql-insert>exec Test.dbo.InsertEmployee ?, ?, ?, ?, ?, ?, ?</sql-insert>
<sql-update>exec Test.dbo.UpdateEmployee ?, ?, ?, ?, ?, ?, ?, ?</sql-update>
<sql-delete>exec Test.dbo.DeleteEmployee ?, ?</sql-delete>
</class>
<sql-query name="SelectEmployee">
<return class="Employee" />
exec Test.dbo.SelectEmployee @employeeId = :EmployeeId
</sql-query>
<sql-query name="SelectEmployeeByStoreId">
<load-collection alias="employeebystore" role="Store.Employees"/>
<return class="Employee" />
exec Test.dbo.SelectEmployeeByStore @storeId = :StoreId
</sql-query>
<sql-query name="SelectEmployeeByLastName">
<return class="Employee" />
exec Test.dbo.SelectEmployeeByLastName @lastName = :LastName
</sql-query>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="Test.dbo" assembly="EmployeeSample" namespace="CSI.Samples.NHibernate">
<class name="Store" table="Store" optimistic-lock="version">
<id name="StoreId" column="StoreId" type="Guid">
<generator class="guid" />
</id>
<version column="Version" type="Int32" name="Version" unsaved-value="0" />
<bag name="Employees" inverse="true" lazy="true" generic="true" table="StoreEmployee">
<key column="StoreId" />
<many-to-many class="Employee" column="EmployeeId" />
<loader query-ref="SelectEmployeeByStoreId" />
<sql-delete>exec Test.dbo.DeleteStoreEmployeeByStore ?</sql-delete>
</bag>
<property column="Name" type="String" name="Name" />
<loader query-ref="SelectStore"/>
<sql-insert>exec Test.dbo.InsertStore ?, ?, ?, ?, ?</sql-insert>
<sql-update>exec Test.dbo.UpdateStore ?, ?, ?, ?, ?, ?</sql-update>
<sql-delete>exec Test.dbo.DeleteStore ?, ?</sql-delete>
</class>
<sql-query name="SelectStore">
<return class="Store" />
exec Test.dbo.SelectStore @storeId = :StoreId
</sql-query>
<sql-query name="SelectStoreByEmployeeId">
<load-collection alias="storebyemployee" role="Employee.Stores"/>
<return class="Store" />
exec Test.dbo.SelectStoreByEmployee @employeeId = :EmployeeId
</sql-query>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Employee employee = session.GetNamedQuery("SelectEmployeeByLastName").SetString("LastName", "Brink").UniqueResult<Employee>();
using (ITransaction transaction = session.BeginTransaction())
{
employee.Stores.Clear();
transaction.Commit();
}
Name and version of the database you are using:MS SQL 2005 SP1
The generated SQL (show_sql=true):Code:
NHibernate: exec Test.dbo.SelectEmployeeByLastName @lastName = @p0; @p0 = 'Brink'
NHibernate: exec Test.dbo.UpdateEmployee @p0, @p1, @p2, @p3, @p4; @p0 = '3', @p1 = '5492006b-04bd-4d8f-bb39-b3cd7b9f96cd', @p2 = 'David', @p3 = 'Brink', @p4 = '2'
NHibernate: DELETE FROM Test.dbo.StoreEmployee WHERE EmployeeId = @p0; @p0 = '11f6d2ec-019c-442c-8feb-84b10912d442'
Debug level Hibernate log excerpt: