views:

48

answers:

2

Hi,

Is there a way in NHibernate to get the foreign key of a child object, without fetching the child object?

EG.

I have User and UserRole. Can I access User.UserRole.UserRoleId without causing another hit on the database to retrieve UserRole?

I realize I can set fetch mode to eager and this will stop it from hitting the database again, but theoretically this shouldn't be needed, as the User table has UserRoldId in it.

Thanks in advance.

A: 

Maybe you could add another field UserRoleId to the User object? Sounds a bit dirty and you'd need to wire up keeping it in sync when changing the UserRole.

rob_g
I thought about this, but I think the ugliness outweighs the performance gains. I would set the fetch mode to eager rather than do this.
Alistair
+2  A: 

Is this field mapped as non-lazy?

It sounds like you want lazy loading. When a Many-to-one is mapped as lazy, a proxy is created which will have only the id field populated. Once you access any property besides the id, it will get loaded from the db.

dotjoe
You are correct. Argh, I thought it worked this way, but then found it not to, however I stuffed up my testing. Thanks!
Alistair