1

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?

1 Answer 1

4

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).

Sign up to request clarification or add additional context in comments.

5 Comments

@slavoo Thanks. I'm still new to stackoverflow. Forgot to use a code block.
Really not sure, why the downvote here?!? I would say that t3sture is correct. So +1 from me, because I'd advise the same.
I think he was just downvoting me as a corrective measure, to make sure I use code blocks in the future. I'd rather believe that than believe that he's just being mean.
+1 - to down voter why does lazy="true" have to be in a code block? Especially as the OP has his emphasised..
Actually, I don't want the collection to load with my object and I don't want it to be lazy too. I don't want NHibernate doing selects, I want the collection to be empty and, when I want the data, I do a fetch in my criteria. Is it possible?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.