-->
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: Over-eager fetching?
PostPosted: Wed May 14, 2008 11:35 am 
Beginner
Beginner

Joined: Sun Nov 18, 2007 10:39 am
Posts: 46
Location: Liverpool, England
Using the mappings below (which I've cut down for clarity), and executing the query specified, I find that NHibernate is generating a single select on the Clients table, as I'd expect, but then is hydrating each of the freightpayers in turn (ie, a query for each) as well. I tried adding the lazy="proxy" attribute to see if that would make a difference, but it didn't help. Can someone tell me what I'm doing wrong?

Hibernate version:
1.2GA

Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Domain" assembly="Saturn.DAO">
<class name="Saturn.DAO.Domain.Client, Saturn.DAO" table="Clients" >
    <composite-id name="ID" class="Saturn.DAO.Types.DBKey">
      <key-property name="Prefix"/>
      <key-property name="ID"/>
    </composite-id>
    <one-to-one name="Freightpayer" lazy="proxy" class="Saturn.DAO.Domain.Freightpayer, Saturn.DAO" />   
</class>
</hibernate-mapping>


Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Domain" assembly="Saturn.DAO">
<class name="Saturn.DAO.Domain.Freightpayer, Saturn.DAO" table="FreightPayers">
    <composite-id name="ID" class="Saturn.DAO.Types.DBKey">
      <key-property name="Prefix"/>
      <key-property name="ID"/>
    </composite-id>
    <one-to-one name="Client" class="Saturn.DAO.Domain.Client, Saturn.DAO" constrained="true" />
</class>
</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():
session.CreateQuery("FROM Client C ORDER BY C.Name").List(Of Domain.Client)()

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 12:31 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
This is the default behavior for fetching joins. There are several ways around that. You can find more information on that in the reference documentation in chapter 15.1 "Fetching Strategies".

In short you can

1) add fetch="join" to the association
2) modify your query to

FROM Client C inner join fetch C.Freightpayer ORDER BY C.Name

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 5:27 pm 
Beginner
Beginner

Joined: Sun Nov 18, 2007 10:39 am
Posts: 46
Location: Liverpool, England
wolli wrote:
This is the default behavior for fetching joins. There are several ways around that. You can find more information on that in the reference documentation in chapter 15.1 "Fetching Strategies".

In short you can

1) add fetch="join" to the association
2) modify your query to

FROM Client C inner join fetch C.Freightpayer ORDER BY C.Name


Hi Wolfgang,
The thing is, I don't want it to fetch the freightpayer at all in this case. So, rather than finding a way to fetch both entities at once, I'm looking for a way to only fetch the client and leave the freightpayer as a proxy.

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 5:50 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
From what I remember, NHibernate 1.x doesn't respect the lazy attribute on one-to-ones. I thought this was going to get fixed in 2.0, but I haven't tried that version yet ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 15, 2008 5:25 am 
Beginner
Beginner

Joined: Sun Nov 18, 2007 10:39 am
Posts: 46
Location: Liverpool, England
Nels_P_Olsen wrote:
From what I remember, NHibernate 1.x doesn't respect the lazy attribute on one-to-ones. I thought this was going to get fixed in 2.0, but I haven't tried that version yet ...


Ah, that would explain what I'm seeing. I'll have to take a look at 2.0

Kev


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.