Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ID for a lazy many-to-one object with NO db access

Tags:

nhibernate

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!

like image 446
Claudio Redi Avatar asked Oct 29 '10 13:10

Claudio Redi


1 Answers

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.

like image 87
Mike Cialowicz Avatar answered Sep 28 '22 06:09

Mike Cialowicz