Hibernate version: 1.2
Mapping documents:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="eStore.Domain.Entities" assembly="eStore.Domain">
<class name="CustomerEntity" table="Customer">
<id name="customerId" column="CustomerId" access="field" type="guid">
<generator class="guid.comb" />
</id>
<property name="FirstName" column="FirstName" type="string" length="50" />
<property name="LastName" column="LastName" type="string" length="50" />
<property name="UserName" column="UserName" type="string" length="50" />
<property name="Password" column="Password" type="string" length="50" />
<property name="IsGold" column="IsGold" type="boolean" />
<bag name="orders" access="field" inverse="true" lazy="false">
<key column="CustomerId" />
<one-to-many class="OrderEntity" not-found="ignore" />
</bag>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
using (ISession session = sessionFactory.OpenSession())
{
CustomerEntity customer = session.CreateCriteria(typeof(CustomerEntity))
.Add(Expression.Eq("UserName", "jyoung"))
.UniqueResult<CustomerEntity>();
}
Name and version of the database you are using: MS SQL 2005The generated SQL (show_sql=true):Code:
SELECT orders0_.CustomerId as CustomerId__1_, orders0_.OrderId as OrderId1_, orders0_.OrderId as OrderId3_0_, orders0_.DateCreated as DateCrea2_3_0_, orders0_.Status as Status3_0_, orders0_.CustomerId as CustomerId3_0_ FROM [Order] orders0_ WHERE orders0_.CustomerId=@p0',N'@p0 uniqueidentifier',@p0='BDB49A49-35B7-43FF-987C-9947014AC5A5'
I have a simple Customer -> Orders mapping.
With the above criteria NH is fetching the customer and ALL orders. To me (a current LLBLGen user and a newbie with NH) this is not expected behavior. I would have thought that only the Customer would have been fetched.
I know I set lazy=false and that if I set lazy=true then it would not be fetched, but I don't want to use lazy loading. The only way I want to get orders from the customer is via the SetFetchMode method on the criteria.
I don't understand the reasoning behind this, can someone explain this behavior to me? Is this the normal behavior? Am I doing something wrong?
Many Thanks,
Joe