开发者

NHibernate bag - real collection items instead of proxies

开发者 https://www.devze.com 2023-01-08 04:00 出处:网络
I am experiencing some strange problems with NHibernate and the usage of proxy items within a bag. My mapping looks something similar to:

I am experiencing some strange problems with NHibernate and the usage of proxy items within a bag. My mapping looks something similar to:

<?xml version="1.0" encoding="utf-8" ?>

<bag name="Markets" table="ITPUserWatchlistMarkets" cascade="none" lazy="false">
  <key column="UserWatchlistId" />
  <many-to-many class="Swan.Domain.Markets.Market, Swan.Domain.Markets" column="MarketId" lazy="proxy" />
</bag>

To my understanding, by setting the many-to-many mapping as lazy="proxy", NHibernate should be creating a proxy object which is different to my Market type, with just the identity key in it and nothing else set on it.

However, when running through debug, I can actually see real Market entities being instantiated and added to the Markets bag, which should not be happening.

Is my mapping incorrect, or does NHibernate create actual entities rather than its own reflected type as the proxy object?

Regards, Alva开发者_Go百科ro


Your understanding is correct on how NHibernate instantiate the proxy and entities. The first time you access any properties of that proxy entity NHibernate will load the entity from the database.

You should try to monitor the sql queries NHibernates creates to see when these queries are being called by using something like NH Profiler or the SQL Profiler.


Upon a bit further investigation, it transpires that the Market class mapping had an explicit lazy="false" property set within the element, overriding the bag's own proxy setting.

All sorted now!

0

精彩评论

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

关注公众号