-->
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.  [ 2 posts ] 
Author Message
 Post subject: Nhibernate Stored Procedure - Errors.
PostPosted: Thu Apr 22, 2010 4:16 pm 
Newbie

Joined: Fri Apr 02, 2010 3:43 pm
Posts: 3
Suppose I have a many-to-one relationship and a Ilist of child objects implemented in my class definition and I use a stored procedure to get all the objects of this class using the IQuery object's List method. Will Nhibernate still load the relationships also for me or is it not possible at this moment.

I am getting an error which says. IndexOutOfRangeException: Last4_5_0_]

I am attaching a copy of the hbm file for future references.

Code:

<?xml version="1.0"?>
   <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="BusinessObjects"
                   assembly="BusinessObjects">

      <!-- Mappings for class 'Product' -->
      <class name="Product" table="dbo.Products" lazy="true">
         <!-- Identity mapping -->
         <id name="Id" unsaved-value="-1">
            <column name="Id" />
            <generator class="increment" />
         </id>
         
         <!-- Simple mappings -->
         <property name="Date_Of_Creation" not-null="false">
            <column name="Date_Of_Creation"/>
         </property>
         
         <property name="Date_Of_Modification" not-null="false">
            <column name="Date_Of_Modification"/>
         </property>

         <many-to-one
            name="Last_Modified_By"
            class="BusinessObjects.User, NHibernateManyToOne"
            column="Last_Modified_By"
            cascade="all"
            fetch="join"
         />

         <many-to-one
            name="Last_Session_Id"
            class="BusinessObjects.BSession, NHibernateManyToOne"
            column="Last_Session_Id"
            cascade="all"
            not-found="ignore"
            fetch="join"
         />

         <property name="Name">
            <column name="Name"/>
         </property>

         <property name="Weight" not-null="false">
            <column name="Weight"/>
         </property>

         <property name="Package_Dimension_Length" not-null="false">
            <column name="Package_Dimension_Length"/>
         </property>

         <property name="Package_Dimension_Breadth" not-null="false">
            <column name="Package_Dimension_Breadth"/>
         </property>

         <property name="Package_Dimension_Height" not-null="false">
            <column name="Package_Dimension_Height"/>
         </property>
         
         <property name="Description" not-null="false">
            <column name="Description"/>
         </property>
         
         <property name="Image_Location_Large" not-null="false">
            <column name="Image_Location_Large"/>
         </property>
         
         <property name="Image_Location_Medium" not-null="false">
            <column name="Image_Location_Medium"/>
         </property>

         <property name="Image_Location_Thumbnail" not-null="false">
            <column name="Image_Location_Thumbnail"/>
         </property>
         
         <property name="Is_Active" not-null="false">
            <column name="Is_Active"/>
         </property>

         <!-- One-to-Many mapping: ProductsPrices-->
         <bag name="ProductsPrices" cascade="all">
            <key column="Product_Id"/>
            <one-to-many class="BusinessObjects.ProductPrice, BusinessObjects"/>
         </bag>

         <!-- One-to-Many mapping: ProductProductCategories -->
         <bag name="ProductCategories" table="dbo.ProductsProductCategories" lazy="false" >
            <key column="Product_Id"></key>
            <many-to-many
                        class="BusinessObjects.ProductCategory, BusinessObjects"
                        column="ProductCategory_Id"
                        fetch="select"
                        ></many-to-many>
         </bag>

         <loader query-ref="App_Products_Get_1" />
         <!--<sql-insert>exec [App_Product_Save]   ?, ?, ?, ?, ?, ?, ?, ?, ?, ? </sql-insert>
         <sql-update>exec [App_Product_Save]   ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? </sql-update>-->

      </class>

      <sql-query name="App_Products_Get_1" >
         <return alias="" class="BusinessObjects.Product, BusinessObjects">
            <return-property name="Id" column="Id"/>
            <return-property name="Date_Of_Creation" column="Date_Of_Creation"/>
            <return-property name="Date_Of_Modification" column="Date_Of_Modification"/>
            <return-property name="Name" column="Name"/>
            <return-property name="Weight" column="Weight"/>
            <return-property name="Package_Dimension_Length" column="Package_Dimension_Length"/>
            <return-property name="Package_Dimension_Breadth" column="Package_Dimension_Breadth"/>
            <return-property name="Package_Dimension_Height" column="Package_Dimension_Height"/>
            <return-property name="Description" column="Description"/>
            <return-property name="Image_Location_Large" column="Image_Location_Large"/>
            <return-property name="Image_Location_Medium" column="Image_Location_Medium"/>
            <return-property name="Image_Location_Thumbnail" column="Image_Location_Thumbnail"/>
            <return-property name="Is_Active" column="Is_Active"/>
         </return>

         exec [App_Products_Get] :Id, :Date_Of_Creation, :Date_Of_Modification, :Last_Modified_By, :Last_Session_Id, :Name, :Description, :Image_Location_Large, :Image_Location_Medium, :Image_Location_Thumbnail, :Is_Active, :ProductCategory_Id, :Date_Of_Creation_Begin, :Date_Of_Creation_End, :Date_Of_Modification_Begin, :Date_Of_Modification_End

      </sql-query>

   </hibernate-mapping>



====================================================================================
====================================================================================
Error Stack Trace

[IndexOutOfRangeException: Last4_5_0_]
System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName) +4841826
System.Data.SqlClient.SqlDataReader.GetOrdinal(String name) +67
NHibernate.Driver.NHybridDataReader.GetOrdinal(String name) +40
NHibernate.Type.NullableType.NullSafeGet(IDataReader rs, String name) +74
NHibernate.Type.NullableType.NullSafeGet(IDataReader rs, String[] names, ISessionImplementor session, Object owner) +56
NHibernate.Type.ManyToOneType.Hydrate(IDataReader rs, String[] names, ISessionImplementor session, Object owner) +83
NHibernate.Persister.Entity.AbstractEntityPersister.Hydrate(IDataReader rs, Object id, Object obj, ILoadable rootLoadable, String[][] suffixedPropertyColumns, Boolean allProperties, ISessionImplementor session) +1209
NHibernate.Loader.Loader.LoadFromResultSet(IDataReader rs, Int32 i, Object obj, String instanceClass, EntityKey key, String rowIdAlias, LockMode lockMode, ILoadable rootPersister, ISessionImplementor session) +451
NHibernate.Loader.Loader.InstanceNotYetLoaded(IDataReader dr, Int32 i, ILoadable persister, EntityKey key, LockMode lockMode, String rowIdAlias, EntityKey optionalObjectKey, Object optionalObject, IList hydratedObjects, ISessionImplementor session) +264
NHibernate.Loader.Loader.GetRow(IDataReader rs, ILoadable[] persisters, EntityKey[] keys, Object optionalObject, EntityKey optionalObjectKey, LockMode[] lockModes, IList hydratedObjects, ISessionImplementor session) +480
NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader resultSet, ISessionImplementor session, QueryParameters queryParameters, LockMode[] lockModeArray, EntityKey optionalObjectKey, IList hydratedObjects, EntityKey[] keys, Boolean returnProxies) +409
NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +1287
NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +106
NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) +172

[ADOException: could not execute query
[ exec [App_Products_Get] @p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12, @p13, @p14, @p15 ]
Name:Id - Value:null Name:Date_Of_Creation - Value:null Name:Date_Of_Modification - Value:null Name:Last_Modified_By - Value:null Name:Last_Session_Id - Value:null Name:Name - Value:null Name:Description - Value:null Name:Image_Location_Large - Value:null Name:Image_Location_Medium - Value:null Name:Image_Location_Thumbnail - Value:null Name:Is_Active - Value:1 Name:Date_Of_Creation_Begin - Value:null Name:Date_Of_Creation_End - Value:null Name:Date_Of_Modification_Begin - Value:null Name:Date_Of_Modification_End - Value:null Name:ProductCategory_Id - Value:null
[SQL: exec [App_Products_Get] @p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12, @p13, @p14, @p15]]
NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) +336
NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) +46
NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes) +155
NHibernate.Loader.Custom.CustomLoader.List(ISessionImplementor session, QueryParameters queryParameters) +52
NHibernate.Impl.SessionImpl.ListCustomQuery(ICustomQuery customQuery, QueryParameters queryParameters, IList results) +259
NHibernate.Impl.SessionImpl.List(NativeSQLQuerySpecification spec, QueryParameters queryParameters, IList results) +232
NHibernate.Impl.SessionImpl.List(NativeSQLQuerySpecification spec, QueryParameters queryParameters) +229
NHibernate.Impl.SqlQueryImpl.List() +243
DAC.Datafetcher.GetAllProductsSearchResult(Decimal Id, DateTime Date_Of_Creation, DateTime Date_Of_Modification, Decimal Last_Modified_By, Decimal Last_Session_Id, String Name, String Description, String Image_Location_Large, String Image_Location_Medium, String Image_Location_Thumbnail, Boolean Is_Active, DateTime Date_Of_Creation_Begin, DateTime Date_Of_Creation_End, DateTime Date_Of_Modification_Begin, DateTime Date_Of_Modification_End, Decimal ProductCategory_Id) in E:\Development\Source Codes\ECommerce\Persistence\Datafetcher.cs:169
Products.Bind_Grid() in e:\Development\Source Codes\ECommerce\Products.aspx.cs:23
Products.Page_Load(Object sender, EventArgs e) in e:\Development\Source Codes\ECommerce\Products.aspx.cs:74
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627


Top
 Profile  
 
 Post subject: Re: Nhibernate Stored Procedure - Errors.
PostPosted: Fri May 07, 2010 2:20 pm 
Newbie

Joined: Fri May 07, 2010 1:53 pm
Posts: 1
I can confirm the same problem. It appears to only happen with a stored procedure (sproc) result set is mapped to a return class. In-line SQL mapped to a return class works absolutely fine.

Relevant part of my mapping file:

Code:
<sql-query name="CompareFlavors">
   <query-param name="UserIdentifier" type="Guid" />
   <query-param name="NewFlavorID" type="bigint" />
   <return class="FlavorComparison">
      <return-property column="ID" name="ID" />
      <return-property column="AssetType" name="AssetType" />
      <return-property column="Name" name="Name" />
      <return-property column="Flavor1Present" name="Flavor1Present" />
      <return-property column="Flavor1Price" name="Flavor1Price" />
      <return-property column="Flavor2Present" name="Flavor2Present" />
      <return-property column="Flavor2Price" name="Flavor2Price" />
   </return>
   exec core.CompareFlavors @UserIdentifier=:UserIdentifier, @NewFlavorID=:NewFlavorID
</sql-query>



Class definition:

Code:
public class FlavorComparison
{
   public virtual long ID { get; set; }
   public virtual string AssetType { get; set; }
   public virtual string Name { get; set; }
   public virtual int Flavor1Present { get; set; }
   public virtual decimal Flavor1Price { get; set; }
   public virtual int Flavor2Present { get; set; }
   public virtual decimal Flavor2Price { get; set; }
}


Example result set:

Code:
ID AssetType Name      Flavor1Present Flavor1Price Flavor2Present Flavor2Price
-- --------- --------- -------------- ------------ -------------- ------------
1  Function  ACH Debit 0              0.00000      0              0.00000


Relevant stack trace:

Code:
NHibernate.ADOException: could not execute query
[ exec core.CompareFlavors @UserIdentifier=@p0, @NewFlavorID=@p1 ]
  Name:UserIdentifier - Value:00000000-0000-0000-0000-000000000000  Name:NewFlavorID - Value:1
[SQL: exec core.CompareFlavors @UserIdentifier=@p0, @NewFlavorID=@p1] ---> System.IndexOutOfRangeException: AssetType39_0_
   at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName)
   at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)
   at NHibernate.Driver.NHybridDataReader.GetOrdinal(String name)
   at NHibernate.Type.NullableType.NullSafeGet(IDataReader rs, String name)
   at NHibernate.Type.ComponentType.Hydrate(IDataReader rs, String[] names, ISessionImplementor session, Object owner)
   at NHibernate.Type.ComponentType.NullSafeGet(IDataReader rs, String[] names, ISessionImplementor session, Object owner)
   at NHibernate.Loader.Loader.GetKeyFromResultSet(Int32 i, IEntityPersister persister, Object id, IDataReader rs, ISessionImplementor session)
   at NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader resultSet, ISessionImplementor session, QueryParameters queryParameters, LockMode[] lockModeArray
, EntityKey optionalObjectKey, IList hydratedObjects, EntityKey[] keys, Boolean returnProxies)
   at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
   at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
   at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
   --- End of inner exception stack trace ---
   at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
   at NHibernate.Impl.SessionImpl.ListCustomQuery(ICustomQuery customQuery, QueryParameters queryParameters, IList results)
   at NHibernate.Impl.SessionImpl.List(NativeSQLQuerySpecification spec, QueryParameters queryParameters, IList results)
   at NHibernate.Impl.SessionImpl.List[T](NativeSQLQuerySpecification spec, QueryParameters queryParameters)
   at NHibernate.Impl.SqlQueryImpl.List[T]()
   at iThryv.App.Core.Data.Dao.HibernateFlavorDao.CompareFlavors(Guid userIdentifier, Int64 comparisonFlavorId) in C:\SRC\ithryv\trunk\trunk\iThryv.App\Source\iThryv.App.Core.Data\Dao\HibernateFlavorDao.cs:line 52
   at _dynamic_iThryv.App.Core.Data.Dao.HibernateFlavorDao.CompareFlavors(Object, Object[] )



This was a real headache to debug. I moved my sproc into a table-valued function in SQL 2008, and then made the SQL "in-line" like this, thinking the problem was with the SPROC engine.

Code:
<sql-query name="CompareFlavors">
   <query-param name="UserIdentifier" type="Guid" />
   <query-param name="NewFlavorID" type="bigint" />
   <return class="FlavorComparison">
      <return-property column="ID" name="ID" />
      <return-property column="AssetType" name="AssetType" />
      <return-property column="Name" name="Name" />
      <return-property column="Flavor1Present" name="Flavor1Present" />
      <return-property column="Flavor1Price" name="Flavor1Price" />
      <return-property column="Flavor2Present" name="Flavor2Present" />
      <return-property column="Flavor2Price" name="Flavor2Price" />
   </return>
   SELECT [ID], [AssetType], [Name], [Flavor1Present], [Flavor1Price], [Flavor2Present], [Flavor2Price]
   FROM [iThryv].[core].[GetFlavorComparison] (:UserIdentifier, :NewFlavorID)
</sql-query>


That didn't do a darn thing, the error was the same -- it kept mangling the column alias and then was still unable to find it via GetOrdinal.

The fix was, surprisingly simple: I simply stopped using "return class" and started using "return alias", which reduced my mapping to this:

Code:
<sql-query name="CompareFlavors">
   <query-param name="UserIdentifier" type="Guid" />
   <query-param name="NewFlavorID" type="bigint" />
   <return alias="fc" class="iThryv.App.Core.Data.Entities.FlavorComparison"/>
   SELECT fc.* FROM [core].[GetFlavorComparison] (:UserIdentifier, :NewFlavorID) fc
</sql-query>


Worked like a charm then. I'm not sure what's wrong about specifying return-properties for a "return class" -- a number of examples on the web show it done this way... but it would appear Nhibnerate then attempts to look up columns by the names you specified, not by the names it decided to mangle the column aliases as.

Happy coding!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.