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.  [ 5 posts ] 
Author Message
 Post subject: Lazy Loading or Join Fetching many-to-one (avoiding selects)
PostPosted: Mon Apr 14, 2008 12:23 pm 
Newbie

Joined: Wed Jun 27, 2007 9:34 am
Posts: 8
When I query rows from my "Address" entity I want to avoid a select to AddressType for each row. I would be fine with this either happening lazily (ideally), or by using a join.

When trying to use a fetch join by running the following query:

Code:
FROM Address a
JOIN FETCH a.Type


I get this error: NHibernate.QueryException: fetch may not be used with scroll() or iterate()

Any assistance would be appreciated. I've scoured the forums but am not sure what I am doing wrong to get either approaches to work.

Let me know if I can provide any additional information.

Thanks,

Sean


Hibernate version: 1.2.0 GA

Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

  <class name="Novus.Nga.Common.ObjectModel.Bed.Address, Novus.Nga.Common.ObjectModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="Address" lazy="true" dynamic-update="true" dynamic-insert="true" >

    <!-- Identifier mapping -->
    <id name="Id" type="Int32" unsaved-value="null" access="field.pascalcase-m">
      <column name="Id" length="4" sql-type="int" not-null="true" unique="true" index="PK_Address"/>
      <generator class="Novus.Nga.Common.Framework.Arch.NHibernate.Id.IdGenerator, Novus.Nga.Common.Framework.Arch.NHibernate">
        <param name="rowIdentifier">Address.ID</param>
      </generator>
    </id>

    <!-- "SysVersion" version mapping -->
    <version name="SysVersion" generated="always" type="SqlTimestamp" access="field.pascalcase-m"/>
   
    <!-- Standard property mapping -->
   
    <property name="Address1" type="String" access="field.pascalcase-m">
      <column name="Address1" length="100" sql-type="varchar" not-null="false"/>
    </property>
   
    <property name="Address2" type="String" access="field.pascalcase-m">
      <column name="Address2" length="100" sql-type="varchar" not-null="false"/>
    </property>
   
    <property name="Address3" type="String" access="field.pascalcase-m">
      <column name="Address3" length="100" sql-type="varchar" not-null="false"/>
    </property>
   
    <property name="City" type="String" access="field.pascalcase-m">
      <column name="City" length="100" sql-type="varchar" not-null="false"/>
    </property>
   
    <property name="State" type="String" access="field.pascalcase-m">
      <column name="`State`" length="2" sql-type="varchar" not-null="false"/>
    </property>
   
    <property name="PostalCode" type="String" access="field.pascalcase-m">
      <column name="PostalCode" length="10" sql-type="varchar" not-null="false"/>
    </property>
   
    <property name="Country" type="String" access="field.pascalcase-m">
      <column name="Country" length="100" sql-type="varchar" not-null="false"/>
    </property>

    <!-- "Sys" column property mapping -->
    <property name="SysCreateDate" type="DateTime" update="false" access="field.pascalcase-m">
      <column name="SysCreateDate" length="8" sql-type="datetime" not-null="true"/>
    </property>
    <property name="SysCreateUserId" type="Int32" update="false" access="field.pascalcase-m">
      <column name="SysCreateUserId" length="4" sql-type="int" not-null="true"/>
    </property>
    <property name="SysUpdateDate" type="DateTime" access="field.pascalcase-m">
      <column name="SysUpdateDate" length="8" sql-type="datetime" not-null="true"/>
    </property>
    <property name="SysUpdateUserId" type="Int32" access="field.pascalcase-m">
      <column name="SysUpdateUserId" length="4" sql-type="int" not-null="true"/>
    </property>

    <!-- Foreign key mappings -->
    <many-to-one name="Type" class="Novus.Nga.Common.ObjectModel.Bed.AddressType, Novus.Nga.Common.ObjectModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" property-ref="Id" access="field.pascalcase-m">
      <column name="AddressTypeId" length="4" sql-type="int" not-null="true"/>
    </many-to-one>

    <bag name="Company_Addresses" inverse="true" lazy="true" cascade="all" collection-type="Novus.Nga.Common.ObjectModel.GenericPersistentEntityList`1[[Novus.Nga.Common.ObjectModel.Bed.Company_Address, Novus.Nga.Common.ObjectModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Novus.Nga.Common.ObjectModel">
      <key column="AddressId"/>
      <one-to-many class="Novus.Nga.Common.ObjectModel.Bed.Company_Address, Novus.Nga.Common.ObjectModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
    </bag>
    <bag name="Person_Addresses" inverse="true" lazy="true" cascade="all" collection-type="Novus.Nga.Common.ObjectModel.GenericPersistentEntityList`1[[Novus.Nga.Common.ObjectModel.Bed.Person_Address, Novus.Nga.Common.ObjectModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Novus.Nga.Common.ObjectModel">
      <key column="AddressId"/>
      <one-to-many class="Novus.Nga.Common.ObjectModel.Bed.Person_Address, Novus.Nga.Common.ObjectModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
    </bag>
   
 
  </class>
</hibernate-mapping>

Name and version of the database you are using: SQL Server 2005

The generated SQL (show_sql=true):
Code:
select address0_.Id as Id30_, address0_.SysVersion as SysVersion30_, address0_.Address1 as Address3_30_, address0_.Address2 as Address4_30_, address0_.Address3 as Address5_30_, address0_.City as City30_, address0_.[State] as State7_30_, address0_.PostalCode as PostalCode30_, address0_.Country as Country30_, address0_.SysCreateDate as SysCrea10_30_, address0_.SysCreateUserId as SysCrea11_30_, address0_.SysUpdateDate as SysUpda12_30_, address0_.SysUpdateUserId as SysUpda13_30_, address0_.AddressTypeId as Address14_30_ from Address address0_

SELECT addresstyp0_.SysKeyword as SysKeyword31_0_, addresstyp0_.SysVersion as SysVersion31_0_, addresstyp0_.Id as Id31_0_, addresstyp0_.Name as Name31_0_, addresstyp0_.Description as Descript5_31_0_, addresstyp0_.[Sequence] as Sequence6_31_0_, addresstyp0_.SysCreateDate as SysCreat7_31_0_, addresstyp0_.SysCreateUserId as SysCreat8_31_0_, addresstyp0_.SysUpdateDate as SysUpdat9_31_0_, addresstyp0_.SysUpdateUserId as SysUpda10_31_0_ FROM AddressType addresstyp0_ WHERE addresstyp0_.Id=@p0

SELECT addresstyp0_.SysKeyword as SysKeyword31_0_, addresstyp0_.SysVersion as SysVersion31_0_, addresstyp0_.Id as Id31_0_, addresstyp0_.Name as Name31_0_, addresstyp0_.Description as Descript5_31_0_, addresstyp0_.[Sequence] as Sequence6_31_0_, addresstyp0_.SysCreateDate as SysCreat7_31_0_, addresstyp0_.SysCreateUserId as SysCreat8_31_0_, addresstyp0_.SysUpdateDate as SysUpdat9_31_0_, addresstyp0_.SysUpdateUserId as SysUpda10_31_0_ FROM AddressType addresstyp0_ WHERE addresstyp0_.Id=@p0

SELECT addresstyp0_.SysKeyword as SysKeyword31_0_, addresstyp0_.SysVersion as SysVersion31_0_, addresstyp0_.Id as Id31_0_, addresstyp0_.Name as Name31_0_, addresstyp0_.Description as Descript5_31_0_, addresstyp0_.[Sequence] as Sequence6_31_0_, addresstyp0_.SysCreateDate as SysCreat7_31_0_, addresstyp0_.SysCreateUserId as SysCreat8_31_0_, addresstyp0_.SysUpdateDate as SysUpdat9_31_0_, addresstyp0_.SysUpdateUserId as SysUpda10_31_0_ FROM AddressType addresstyp0_ WHERE addresstyp0_.Id=@p0

SELECT addresstyp0_.SysKeyword as SysKeyword31_0_, addresstyp0_.SysVersion as SysVersion31_0_, addresstyp0_.Id as Id31_0_, addresstyp0_.Name as Name31_0_, addresstyp0_.Description as Descript5_31_0_, addresstyp0_.[Sequence] as Sequence6_31_0_, addresstyp0_.SysCreateDate as SysCreat7_31_0_, addresstyp0_.SysCreateUserId as SysCreat8_31_0_, addresstyp0_.SysUpdateDate as SysUpdat9_31_0_, addresstyp0_.SysUpdateUserId as SysUpda10_31_0_ FROM AddressType addresstyp0_ WHERE addresstyp0_.Id=@p0

SELECT addresstyp0_.SysKeyword as SysKeyword31_0_, addresstyp0_.SysVersion as SysVersion31_0_, addresstyp0_.Id as Id31_0_, addresstyp0_.Name as Name31_0_, addresstyp0_.Description as Descript5_31_0_, addresstyp0_.[Sequence] as Sequence6_31_0_, addresstyp0_.SysCreateDate as SysCreat7_31_0_, addresstyp0_.SysCreateUserId as SysCreat8_31_0_, addresstyp0_.SysUpdateDate as SysUpdat9_31_0_, addresstyp0_.SysUpdateUserId as SysUpda10_31_0_ FROM AddressType addresstyp0_ WHERE addresstyp0_.Id=@p0


Top
 Profile  
 
 Post subject: Lazy Loading...
PostPosted: Wed Apr 16, 2008 2:52 pm 
Newbie

Joined: Wed Apr 16, 2008 2:44 pm
Posts: 1
My understanding is that you should be able to Lazy Load on many to one relationships like you are doing, however I have not been able to figure it out either.

Anybody with some insight or a working example (hbm) would be great!

Sorry I can't be more help to you tseanf, but hopefully this will bump the post for the both of us.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 21, 2008 4:20 pm 
Newbie

Joined: Wed Jun 27, 2007 9:34 am
Posts: 8
Any general insight on how to make many-to-one properties lazy load or use a join to fetch?


Top
 Profile  
 
 Post subject: Figured out why my many-to-one's were not lazy loading...
PostPosted: Mon Apr 28, 2008 8:27 pm 
Newbie

Joined: Wed Jun 27, 2007 9:34 am
Posts: 8
In our model most of our many-to-ones use the property-ref attribute since our Primary Key is not usually our FK.

In the EntityType class there is a specific condition which says if the reference is not the primary key, load the entity.

Does anyone know if that is just a current known limitation, or is there a great reason why they can not be lazy read?

Thanks,

Sean


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 28, 2008 8:41 pm 
Newbie

Joined: Wed Jun 27, 2007 9:34 am
Posts: 8
Yep, looks like my issue is a JIRA ticket and marked to get fixed in 2.0.0

http://jira.nhibernate.org:8080/jira/browse/NH-1125


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