[Note] See my update further down for the real question. I’ve left the original one in case it’s useful to anyone…
Just trying to get this working. It looks like the Kats/Kittens example from the docs as far as I can tell, but I get a NullReferenceException when I try to use it.
CustomerOrder order = CurrentSession.CreateQuery(
@"select new CustomerOrder(Name)
from CustomerOrder o
left join fetch o.OrderItems as items
where o.Id = :Id")
.SetParameter("Id", id)
.UniqueResult<CustomerOrder>();
If I omit the join fetch (to just bring back ‘CustomerOrder’ withoutthe OrderItems) it works. My CustomerOrder has a constructor that takes a String for ‘Name’ and there are OrderItems records in the database for the order I am retrieving.
What am I doing wrong?
[Update]
I’ve just been reading that what I am trying to do doesn’t really make sense since I am instantiating a new object with some values and what I am doing is effectively trying to fetch the OrderItems as though they belonged to the newly instantiated object, which they don’t.
What I need is an Order with just the Name field populated, along with it’s collection or OrderItem children, ideally with only their ‘Description’ field populated. I want to do this so I am not retrieving all columns back as HQL would normally do (for performance reasons).
What’s the best way to do this?
Thanks
Thanks to JB Nizet and Rippo for their insights. I tried a few things and performance is fine (for my purposes anyway). If it ever does cause problems I will look at ResultTransformer but I’m sure there are other aspects of my app’s performance that will have a far greater impact on how quickly it runs (which I can optimize via profiling etc etc) than the number of columns being retrieved.