rossisdead
rossisdead

Reputation: 2108

Fetching a lazy loaded property in NHibernate that *isn't* a reference

I have an entity with a binary column that's set to lazy load in the mapping. In some cases, though, we want to get the entity along with the binary data at the same time. I tried using Linq.Fetch(x => x.BinaryData), but that gives an invalid join exception. Understandable, considering it shouldn't be a join in the first place. Is there a way to get this working? I'm using NHibernate 3.1

This is the mapping:

Map(x => x.BinaryData)
   .CustomSqlType("image")
   .Length(int.MaxValue)
   .Not.Nullable()
   .LazyLoad();  // Wanna make sure we don't kill the app by loading the image data when we don't need it.

This is the fetching:

Linq.Where(x => x.Id == id).Fetch(x => x.BinaryData).FirstOrDefault();

Upvotes: 2

Views: 1781

Answers (2)

mathieu
mathieu

Reputation: 31202

This looks like to be not possible at the moment : https://nhibernate.jira.com/browse/NH-2888

So, You have to use HQL :

var post = session.CreateQuery("from Post fetch all properties")
    .SetMaxResults(1)
    .UniqueResult<Post>();

Source : http://ayende.com/blog/4377/nhibernate-new-feature-lazy-properties

Upvotes: 2

Nikolay
Nikolay

Reputation: 3828

In HQL you can use fetch all properties to eagerly load lazy property. But in NH3.1 it is not yet implemented for Linq queries. As I know this bug is in NHibernate Jira so you can check if it is resolved or you can fix it yourself. For our company prototype i fixed this bug, but I did so in very brute-force way so i didn't send patch to NHibernate project

Upvotes: 2

Related Questions