NHibernate version: 1.2.0.GA
Mapping documents: nhibernate-mapping-2.2
Name and version of the database you are using: MS SQLServer 2005
Hi! I need some help to understand how to implement lazy load with relationships.
I Have a class named "Usuario", this class has two properties that are "Historicos" type IList<Historico> and "Acessos" type IList<Acessos>.
It's all OK, but when I look to the log, I see that nHibernate do 3 queries, one to get the data for "Usuario" and the 2 other to fill the collections "Historicos" and "Acessos".
I already marked as "lazy=true", but nHibernate still doing the 2 other queries.
What I'm missing? Bellow is the mapping file and the nHibernate log.
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="UserAdmin.Core.Domain.Usuario, UserAdmin.Core" table="Usuario" lazy="true">
<id name="Id" column="IdUsuario" unsaved-value="0">
<generator class="identity" />
</id>
<property name="Nome" column="Nome" not-null="true" />
<property name="Login" column="Login" not-null="true" />
<property name="Senha" column="Senha" not-null="true" />
<property name="DtAlteracaoSenha" column="DtAlteracaoSenha" />
<property name="DtUltimoAcesso" column="DtUltimoAcesso" />
<property name="DtNascimento" column="DtNascimento" not-null="true" />
<property name="EMail" column="EMail" not-null="true" />
<many-to-one name="Status" column="IdStatusUsuario"
class="UserAdmin.Core.Domain.StatusUsuario, UserAdmin.Core" not-null="true" />
<bag name="Historicos" inverse="true" lazy="true" generic="true" cascade="all" table="Historico" >
<key column="IdUsuario" />
<one-to-many class="UserAdmin.Core.Domain.Historico, UserAdmin.Core" />
</bag>
<bag name="Acessos" inverse="true" lazy="true" generic="true" cascade="all" table="Acesso">
<key column="IdUsuario" />
<one-to-many class="UserAdmin.Core.Domain.Acesso, UserAdmin.Core" />
</bag>
</class>
</hibernate-mapping>
I want this query:
14:45:48.185 [11] DEBUG NHibernate.SQL - SELECT this_.IdUsuario as IdUsuario5_0_, this_.Nome as Nome5_0_, this_.Login as Login5_0_, this_.Senha as Senha5_0_, this_.DtAlteracaoSenha as DtAltera5_5_0_, this_.DtUltimoAcesso as DtUltimo6_5_0_, this_.DtNascimento as DtNascim7_5_0_, this_.EMail as EMail5_0_, this_.IdStatusUsuario as IdStatus9_5_0_ FROM dbuseradmin.dbo.Usuario this_ WHERE this_.Login = 'fauresco'
Why nHibernate is doing this other 2 queries since lazy=true?
14:45:50.523 [11] DEBUG NHibernate.SQL - SELECT historicos0_.IdUsuario as IdUsuario__1_, historicos0_.IdHistorico as IdHistor1_1_, historicos0_.IdHistorico as IdHistor1_2_0_, historicos0_.DtRegistro as DtRegistro2_0_, historicos0_.Informacao as Informacao2_0_, historicos0_.IdEventoHistorico as IdEvento4_2_0_, historicos0_.IdUsuario as IdUsuario2_0_ FROM dbuseradmin.dbo.Historico historicos0_ WHERE historicos0_.IdUsuario=@p0; @p0 = '1'
14:45:50.679 [11] DEBUG NHibernate.SQL - SELECT acessos0_.IdUsuario as IdUsuario__1_, acessos0_.IdAcesso as IdAcesso1_, acessos0_.IdAcesso as IdAcesso0_0_, acessos0_.IdUsuario as IdUsuario0_0_, acessos0_.IdPerfil as IdPerfil0_0_ FROM dbuseradmin.dbo.Acesso acessos0_ WHERE acessos0_.IdUsuario=@p0; @p0 = '1'
Thanks!!