-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Mapped subclass only return base class, Polymorphism broken.
PostPosted: Wed Sep 08, 2010 6:54 am 
Newbie

Joined: Wed Sep 08, 2010 6:38 am
Posts: 9
I have a class hierachy mapped which works fine when retrieved with a NH session using Fluent.

However where another mapped class has a reference to the base class only a proxy for the base class is created not the correct subclass which is defined by the discriminator value for the object with the specified Id. I guess the problem is that the references method does not know it needs to create the sub class.

Is there anyway to always create an object as it's actual type based on the discriminator value.


Top
 Profile  
 
 Post subject: Re: Mapped subclass only return base class, Polymorphism broken.
PostPosted: Sat Sep 11, 2010 12:03 am 
Newbie

Joined: Tue Aug 31, 2010 10:22 pm
Posts: 3
You need to post mappings or more details.

If I have two classes:

SubClass1
SubClass2

that are both derived from:

Base1

and I create a third class Class3 that references the two subclasses as

Base1 SubClass1
{
get, set
}

and

Base1 SubClass2
{
get, set
}

then my class3 correctly returns the SubClass1 or SubClass2 as set in the properties. Well specifically what it returns is an instance of Base1 since that is how the property is defined, but that instance can then be cast to the proper subclass.

Is this what you are trying to do? If so it should work - there may be a problem with your mappings


Top
 Profile  
 
 Post subject: Re: Mapped subclass only return base class, Polymorphism broken.
PostPosted: Mon Sep 20, 2010 11:06 am 
Newbie

Joined: Wed Sep 08, 2010 6:38 am
Posts: 9
Class1 : Member
{
}

Class2 : Member

Here is the class

public class Link : ValueObject<Link>
{
protected virtual int Id { get; set; }

public virtual Member LinkFrom{get;protected set;}
public virtual Member LinkTo{get;protected set;}


Here is my map

public class LinkMap : ClassMap<Link>
{
public LinkMap()
{
Id(Reveal.Member<Link>("Id"), "LinkId")
.GeneratedBy.Native();

References(x => x.LinkFrom, "LinkFrom");
References(x => x.LinkTo, "LinkTo");
References(x => x.Definition).Columns("linkType", "reverselinkType").Fetch.Join();

Map(x=>x.LinkDate);
Table("Links");
}
}

Here is the hbm produced by fluent

<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" mutable="true" name="DTE.Business.Domain.MembershipCourses.Link, DTE.Business, Version=0.0.2.0, Culture=neutral, PublicKeyToken=null" table="Links">
<id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="LinkId" />
<generator class="native" />
</id>
<property name="LinkDate" type="System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="LinkDate" />
</property>
<many-to-one class="DTE.Business.Domain.MembershipCourses.Member, DTE.Business, Version=0.0.2.0, Culture=neutral, PublicKeyToken=null" name="LinkFrom">
<column name="LinkFrom" />
</many-to-one>
<many-to-one class="DTE.Business.Domain.MembershipCourses.Member, DTE.Business, Version=0.0.2.0, Culture=neutral, PublicKeyToken=null" name="LinkTo">
<column name="LinkTo" />
</many-to-one>
<many-to-one class="DTE.Business.Domain.MembershipCourses.LinkType, DTE.Business, Version=0.0.2.0, Culture=neutral, PublicKeyToken=null" fetch="join" name="Definition">
<column name="linkType" />
<column name="reverselinkType" />
</many-to-one>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Mapped subclass only return base class, Polymorphism broken.
PostPosted: Tue Sep 21, 2010 3:03 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
http://nhforge.org/doc/nh/en/index.html#performance-fetching-proxies

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Mapped subclass only return base class, Polymorphism broken.
PostPosted: Thu Sep 23, 2010 11:19 am 
Newbie

Joined: Wed Sep 08, 2010 6:38 am
Posts: 9
Thanks for the reply.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.