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: [solved] Help with <bag> and lazy load
PostPosted: Fri Nov 09, 2007 2:04 pm 
Newbie

Joined: Fri Nov 09, 2007 1:49 pm
Posts: 2
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!!


Last edited by fauresco on Fri Nov 09, 2007 4:50 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Found!
PostPosted: Fri Nov 09, 2007 4:49 pm 
Newbie

Joined: Fri Nov 09, 2007 1:49 pm
Posts: 2
OK, I figured out...
In my class "Usuario" I've changed:

Code:
/// <summary>
/// Históricos relacionados a este usuário.
/// </summary>
public virtual IList<Historico> Historicos
{
        //get { return new List<Historico>(historicos).AsReadOnly(); }
        //protected set { historicos = value; }
        get { return historicos; }
        set { historicos = value; }
}


It works now!
Thanks anyway.


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.