问题
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
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/4826353/nhibernate-3-lazy-properties-and-eager-queries