开发者

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

开发者 https://www.devze.com 2022-12-24 18:36 出处:网络
Is there a way in NHi开发者_JAVA技巧bernate to get the foreign key of a child object, without fetching the child object?

Is there a way in NHi开发者_JAVA技巧bernate 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号