-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Trouble with many-to-many and stored procedures
PostPosted: Mon Feb 26, 2007 12:22 pm 
Newbie

Joined: Sun Feb 25, 2007 6:59 pm
Posts: 8
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:


Top
 Profile  
 
 Post subject: Additional question
PostPosted: Mon Feb 26, 2007 6:39 pm 
Newbie

Joined: Sun Feb 25, 2007 6:59 pm
Posts: 8
I'm also getting a null reference exception exception thrown when I attempt to lazy load either Employee.Stores or Store.Employees. If I remove the <loader query-ref=""> tag, both collections load without the exception.

Thanks for looking.

Code to generate the exception
Code:
int count = session.Get<Employee>(employee.EmployeeId).Stores.Count;


Exception dump
Code:
NHibernate.LazyInitializationException: Failed to lazily initialize a collection ---> NHibernate.ADOException: could not initialize collection: [CSI.Samples.NHibernate.Employee.Stores#7d5afa05-8152-44ed-ac42-63e9bdbd8843] ---> System.NullReferenceException: Object reference not set to an instance of
an object.
   at NHibernate.Loader.DefaultEntityAliases..ctor(IDictionary userProvidedAliases, ILoadable persister, String suffix)
   at NHibernate.Loader.Custom.ColumnEntityAliases..ctor(IDictionary returnProperties, ILoadable persister, String suffix)
   at NHibernate.Loader.Custom.SQLQueryReturnProcessor.GenerateCustomReturns(Boolean queryHadAliases)
   at NHibernate.Loader.Custom.SQLCustomQuery..ctor(ISQLQueryReturn[] queryReturns, String sqlQuery, ICollection additionalQuerySpaces, ISessionFactoryImplementor factory)
   at NHibernate.Impl.SessionImpl.List(NativeSQLQuerySpecification spec, QueryParameters queryParameters, IList results)
   at NHibernate.Impl.SessionImpl.List(NativeSQLQuerySpecification spec, QueryParameters queryParameters)
   at NHibernate.Impl.SqlQueryImpl.List()
   at NHibernate.Persister.Collection.NamedQueryCollectionInitializer.Initialize(Object key, ISessionImplementor session)
   at NHibernate.Persister.Collection.AbstractCollectionPersister.Initialize(Object key, ISessionImplementor session)
   --- End of inner exception stack trace ---
   at NHibernate.Persister.Collection.AbstractCollectionPersister.Initialize(Object key, ISessionImplementor session)
   at NHibernate.Impl.SessionImpl.InitializeCollection(IPersistentCollection collection, Boolean writing)
   at NHibernate.Collection.AbstractPersistentCollection.Initialize(Boolean writing)
   --- End of inner exception stack trace ---
   at NHibernate.Collection.AbstractPersistentCollection.Initialize(Boolean writing)
   at NHibernate.Collection.AbstractPersistentCollection.Read()
   at NHibernate.Collection.Generic.PersistentGenericBag`1.get_Count()


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 1:58 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Looks like many-to-many custom SQL support is broken. If you submit a bug report to JIRA with a test case, I'll look into it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 12:01 pm 
Newbie

Joined: Sun Feb 25, 2007 6:59 pm
Posts: 8
Thanks for looking at this.

Issue with sample project and tables located at: http://jira.nhibernate.org/browse/NH-925

Let me know if you need any more information.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.