NHibernate 3 lazy properties and eager queries

I'm using NHibernate 3.0.0 and the new feature lazy properties. However, in some queries I want to eagerly load the properties and I would like the query to result in non-proxied entity instances. I also prefer to use the LINQ provider or QueryOver. So my two questions related to this are:

  • I know that eager loading of properties can be achieved in HQL with "fetch all properties" but is it possible to do the same with the LINQ provider or QueryOver?
  • If I execute an HQL query with "fetch all properties" the entities in the result are indeed eagerly loaded but they are still proxied. Is there a way of avoiding this?
  • Cheers


    For question 1 : The answer is no, 'fetch all properties' as described in http://ayende.com/blog/4377/nhibernate-new-feature-lazy-properties is currently only supported in HQL.

    I do remember seeing in the NH Google Discussions somewhere, I'm unable to find the thread. Will update answer if I can find it today. But as of the current 3.1 release it's only in HQL.

    For question 2 : I don't know if there is a way to avoid this, I usually transform them to DTO's and serialize the DTO, not the NH Poco, when using WCF. Leonardo's link looks interesting and may be able to help.


    You can use this http://davybrion.com/blog/2009/03/must-everything-be-virtual-with-nhibernate/ to disable de lazy load of your classes (no proxies)
    You can use this http://nhibernate.info/doc/nh/en/index.html#querycriteria-dynamicfetching to eager load your properties
    Or you can try this http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx and still use the proxies.

    链接地址: http://www.djcxy.com/p/50254.html

    上一篇: 红宝石中的死代码检测

    下一篇: NHibernate 3懒惰的属性和渴望的查询