Alistair
Alistair

Reputation: 1979

Nhibernate: Don't fetch when accessing a child objects primary key

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.

Upvotes: 2

Views: 382

Answers (2)

dotjoe
dotjoe

Reputation: 26940

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.

Upvotes: 2

Rob Gray
Rob Gray

Reputation: 3266

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.

Upvotes: -1

Related Questions