tags:

views:

14

answers:

1

I wonder if this is possible with any Nhibernate version.

I have a class A with a property of class B connected by a lazy many-to-one relation. I'd like to get A.B.Id without going to the database (I mean, without getting the whole B entity). Is this possible?

Thanks!

+1  A: 

Just do it! Hibernate is smart enough to not deep-load objects unless you need their other properties, so calling A.getB().getId() shouldn't result in the deep-loading of B (it'll use the id of B stored in A).

Here's a website that explains the concept in a bit more detail: Getting the Id from Lazy Loaded Object Using Annotations in Hibernate

Try it out and see for yourself.

Mike Cialowicz
@Mike: I thought this was not the case taking into account what I found searching for this but I tested and it works perfectly. Nice surprise. Thanks!
Claudio Redi