This behavior is expected, an there's a great post by Ayende on it:
http://ayende.com/Blog/archive/2009/09/03/answer-the-lazy-loaded-inheritance-many-to-one-association-orm.aspx

<http://ayende.com/Blog/archive/2009/09/03/answer-the-lazy-loaded-inheritance-many-to-one-association-orm.aspx>BTW,
it *does* respect polymorphism, what it doesn't respect (because it's
technically impossible) is typing.
If you have any virtual methods on DogHouse and you override them in
SmallDogHouse, calling them will correctly use the SmallDogHouse version.

   Diego


On Tue, Jan 12, 2010 at 17:26, Eric J. Peters <[email protected]> wrote:

>
> I have what I believe to be a simple problem: References don't seem to
> be respecting polymorphism for me.
>
> Given the following objects and mappings:
>
>    public class Dog
>    {
>        public virtual int? Id { get; set; }
>        public virtual string Name { get; set; }
>
>        public virtual DogHouse House { get; set; }
>    }
>
>    public class DogHouse
>    {
>        public virtual int? Id { get; set; }
>        public virtual string Name { get; set; }
>    }
>
>    public class SmallDogHouse : DogHouse
>    {
>    }
>
> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-
> access="property" auto-import="true" default-cascade="none" default-
> lazy="true">
>  <class xmlns="urn:nhibernate-mapping-2.2" name="FNHTB.Entities.Dog,
> FNHTB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
> table="Dogs">
>    <id name="Id" type="System.Nullable`1[[System.Int32, mscorlib,
> Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]],
> mscorlib, Version=2.0.0.0, Culture=neutral,
> PublicKeyToken=b77a5c561934e089">
>      <column name="Id" />
>      <generator class="native" />
>    </id>
>    <property name="Name" type="System.String, mscorlib,
> Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
>      <column name="Name" />
>    </property>
>    <many-to-one class="FNHTB.Entities.DogHouse, FNHTB,
> Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" foreign-
> key="House_Id" name="House">
>      <column name="House_id" />
>    </many-to-one>
>  </class>
> </hibernate-mapping>
>
> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-
> access="property" auto-import="true" default-cascade="none" default-
> lazy="true">
>  <class xmlns="urn:nhibernate-mapping-2.2"
> name="FNHTB.Entities.DogHouse, FNHTB, Version=1.0.0.0,
> Culture=neutral, PublicKeyToken=null" table="DogHouses">
>    <id name="Id" type="System.Nullable`1[[System.Int32, mscorlib,
> Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]],
> mscorlib, Version=2.0.0.0, Culture=neutral,
> PublicKeyToken=b77a5c561934e089">
>      <column name="Id" />
>      <generator class="native" />
>    </id>
>    <discriminator type="String">
>      <column name="DogHouseStyle" />
>    </discriminator>
>    <property name="Name" type="System.String, mscorlib,
> Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
>      <column name="Name" />
>    </property>
>    <subclass name="FNHTB.Entities.SmallDogHouse, FNHTB,
> Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" discriminator-
> value="S" />
>    <subclass name="FNHTB.Entities.LargeDogHouse, FNHTB,
> Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" discriminator-
> value="L" />
>  </class>
> </hibernate-mapping>
>
> When I run code to get the dog house (reference) from the dog, it
> always returns the proxy of the base type (DogHouse), and not a proxy
> on the appropriate subtype (SmallDogHouse) as I would expect, even
> after referencing the type to ensure lazy loading has triggered.
>
> Here's one small test snippet:
>
>            using (var session = dogPersister.OpenSession())
>            {
>                SmallDogHouse smallDoghouse = new SmallDogHouse()
> { Name = "Small Doghouse" };
>                smallDogHousePersister.Create(session, smallDoghouse);
>
>                LargeDogHouse largeDoghouse = new LargeDogHouse()
> { Name = "Large Doghouse" };
>                largeDogHousePersister.Create(session, largeDoghouse);
>
>                Dog dog = new Dog() { Name = "Fido", House =
> smallDoghouse };
>                dogPersister.Create(session, dog);
>            }
>
>            using (var session = dogPersister.OpenSession())
>            {
>                foreach (var dog in dogPersister.FindAll(session))
>                {
>                    string test = dog.House.Name;
>                    Console.WriteLine(dog.House.GetType());
>                }
>            }
>
> Funny thing is, it works fine for very similar collections of objects,
> or non-lazy loaded objects, it seems to be just this reference/many-to-
> one case that fails for me.
>
> Can somebody tell me what I'm missing/doing wrong?  I'm baffled.
> Thanks!
> -Eric.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>
>
>
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.

Reply via email to