views:

83

answers:

2

Hi all,

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, Alvaro

+1  A: 

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.

adriaanp
A: 

Many thanks for clarifying that.

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!

Regards, Alvaro

Alvaro