NHibernate creates proxy via session.Load(), but not via Linq or Criteria API

后端 未结 1 1440
挽巷
挽巷 2020-12-17 17:26

I have an odd problem in my current project. Lazy loading for queries does not work. When I query a list, nhibernate fetches all associations separately.

I extracted

相关标签:
1条回答
  • 2020-12-17 18:10

    After quite some more research, I found the answers. Answers, because there are many things that can prevent lazy loading in NHibernate.

    1. Query vs. session.Load: When fetching an item via session.Load() you get a proxy. But as soon as you access any property, lets say the Url, the object is fetched including all it's associations that doesn't support lazy loading.

    2. property-ref: Lazy loading only works over a objects id. When an property-association is resolved via a different column in the target entity, NH fetches it eagerly. Not that this wouldn't be possible, it's just not implemented: Bug

    3. not-found="ignore" allows invalid foreign keys, that is, if the referenced entity isn't found NH will init the property with null. NH doesn't intercept the property-access for lazy loading, but instead assignes a object proxy. With not-found="ignore" it can't decide if the property should be set to null or a proxy for the given, possibly invalid, foreign key. This could possibly be solved by intercepting the property access.

    4. When disabling not-found="ignore" and property-ref the schema export would generate constraints that enforce a circular reference. Not good! The correct mapping would then be a constrained one-to-one relationship, where the key for HippoAccountSync must have a generator foreign.

    Resources

    • Select statement issued for each not-found=ignore
    • Lazy-load conflicts with Property-ref in Many-to-One Mapping
    • Google groups discussion
    0 讨论(0)
提交回复
热议问题