Raiiy
Raiiy

Reputation: 305

NHibernate Lazy Loading = false

I set lazy="false" to a collection and fetch="select", but I dont understand why NHibernate keeps loading my collection.

This is my mapping:

 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="Ortopedia.Entidades" assembly="Ortopedia">
  <class name="Especialidade" table="TB_ESPECIALIDADE">
    <id name="Id">
      <column name="ID_ESPECIALIDADE" not-null="true" />
      <generator class="increment" />
    </id>
    <property name="Nome" column="NOME" not-null="true" length="50" />
    <set inverse="true" name="SubEspecialidades" cascade="none" fetch="select" lazy="false" >
      <key column="ID_ESPECIALIDADE" />
      <one-to-many class="Ortopedia.Entidades.SubEspecialidade" />
    </set>
  </class>
</hibernate-mapping>

And this is the code I'm using to list the data:

ICriteria criteria = session.CreateCriteria(typeof(T));
criteria.SetMaxResults(1000);
IList<T> list = criteria.List<T>();
return list;

NHibernate loads my SubEspecialidades property, I dont want it to load. What am I missing here?

Upvotes: 1

Views: 1923

Answers (1)

t3sture
t3sture

Reputation: 66

If you don't want SubEspecialidades to load initially, you should be using:

lazy="true"

Lazy loading means that the collection will not be fetched from the database until you access it in your code. So if you set it to false, it will be fetched along with its parent object (whatever owns the collection).

Upvotes: 4

Related Questions