-->
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.  [ 4 posts ] 
Author Message
 Post subject: Need help with joined-subclass and polymorphic association
PostPosted: Fri Jul 29, 2005 12:06 pm 
Newbie

Joined: Fri Jul 29, 2005 11:48 am
Posts: 11
My mapping file is below. I created a joined-subclass and in the subclass I added a polymorphic association back to the parent class, just like it's described in the Hibernate documentation.

When I try to create a child object and associated it back to a parent and save it :
Code:
Owner owner = GetOwnerFromRequest();
Investor investor = new Investor();
investor.Owner = owner;
Save(investor);


I get this error:
Quote:
System.Data.SqlClient.SqlException: Column name 'InvestorId' appears more than once in the result column list.


I still don't fully understand subclass and polymorphic associations in Hibernate yet. Can you guys offer some suggestions or directions? Thanks.

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Owner" table="Owner">
      <id name="Id" column="OwnerId" type="Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
         <generator class="guid"/>
      </id>
      <property column="Prefix" type="String" name="Prefix" length="10" />
      <property column="FirstName" type="String" name="FirstName" length="50" />
      <property column="MiddleInitial" type="String" name="MiddleInitial" length="10" />
      <property column="LastName" type="String" name="LastName" length="50" />
      <property column="Salutation" type="String" name="Salutation" length="50" />
      <property column="Title" type="String" name="Title" length="50" />
      <property column="Company" type="String" name="Company" length="50" />
      <property column="WorkPhone1" type="String" name="WorkPhone1" length="50" />
      <property column="WorkPhone2" type="String" name="WorkPhone2" length="50" />
      <property column="HomePhone1" type="String" name="HomePhone1" length="50" />
      <property column="HomePhone2" type="String" name="HomePhone2" length="50" />
      <property column="CellPhone" type="String" name="CellPhone" length="50" />
      <property column="Fax" type="String" name="Fax" length="50" />
      <property column="Email" type="String" name="Email" length="50" />
      <property column="DoNotCall" type="Nullables.NHibernate.NullableBooleanType, Nullables.NHibernate" name="DoNotCall" />
      <property column="DoNotMail" type="Nullables.NHibernate.NullableBooleanType, Nullables.NHibernate" name="DoNotMail" />
      <property column="DoNotEmail" type="Nullables.NHibernate.NullableBooleanType, Nullables.NHibernate" name="DoNotEmail" />
      <property column="DoNotFax" type="Nullables.NHibernate.NullableBooleanType, Nullables.NHibernate" name="DoNotFax" />
      <property column="Reports" type="String" name="Reports" length="50" />
      
      <bag name="Properties" lazy="true" table="PropertyOwned" cascade="all" inverse="true">
         <key column="OwnerId" />
         <composite-element class="PropertyOwned">
            <parent name="Owner" />
            <property column="IsPrimaryOwner" type="Nullables.NHibernate.NullableBooleanType, Nullables.NHibernate" name="IsPrimaryOwner" />
            <property column="FromDate" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" name="FromDate" />
            <property column="ToDate" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" name="ToDate" />
            <property column="OwnerType" type="String" name="OwnerType" length="50" />
            <property column="NameOnFile" type="String" name="NameOnFile" length="50" />
            <many-to-one name="Property" column="PropertyId" class="Property" />
         </composite-element>
      </bag>
            
      <many-to-one name="CreatedBy" class="User" column="CreatedBy" />
      <property column="CreateDate" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" name="CreateDate" />
      <many-to-one name="UpdatedBy" class="User" column="UpdatedBy" />
      <property column="UpdateDate" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" name="UpdateDate" />
      
      <joined-subclass name="Investor" table="Investor">
         <key column="InvestorId" />
         <many-to-one name="Owner" column="InvestorId" class="Owner" />
         <property name="Type" column="InvestorType" type="String" length="50" />
         <bag name="Offers" lazy="true" inverse="true">
            <key column="InvestorId" />
            <one-to-many class="Offer" />
         </bag>
      </joined-subclass>
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 5:32 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You have two different properties mapped to the InvestorId column, looks like it's your problem:
Code:
      <joined-subclass name="Investor" table="Investor">
         <key column="InvestorId" />
         <many-to-one name="Owner" column="InvestorId" class="Owner" />
         ...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 6:00 pm 
Newbie

Joined: Fri Jul 29, 2005 11:48 am
Posts: 11
the second property:
Code:
<many-to-one name="Owner" column="InvestorId" class="Owner" />


Is for the association back to the parent object, and its in the Hibernate documentation http://www.hibernate.org/hib_docs/reference/en/html/inheritance.html.

Otherwise how can i create a row in the subclass table and associate its PKFK back to the row in the parent class table?

I kind of came up with a work around, by creating another class and adding a one-to-one mapping between the new class and my parent class. The mapping file is below:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="OwnerInvestor" table="Investor">
      <id name="Id" column="InvestorId" type="Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
         <generator class="foreign">
            <param name="property">Owner</param>
         </generator>
      </id>
      <one-to-one name="Owner" class="Owner" constrained="true" />
   </class>
</hibernate-mapping>


Now if I do:
Code:
OwnerInvestor ownerInvestor = new OwnerInvestor();
ownerInvestor.Owner = owner;
Save(ownerInvestor);

I just created my association back to my parent object. Its very messy though.

Theretically, now I can retreive my Investor object like normal, but I'm getting this error:
Quote:
(loading object was of wrong class)

which is probably a totally different error all together.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 02, 2005 4:08 pm 
The documentation I was looking at was for Hibernate 2.1, and I could not find a reference to it in the 2.0.3 documentation. So it looks like its a feature that has not been added to NHibernate yet. But luckly joined-subclass is virtually the same as an one-to-one PKFK relation between 2 tables, so my work around works.

I'm experiencing another problem though. If I query for a subclass after i already queried for the parent class in the same session, it will always return the parent class. Is there anyway around this?


Top
  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.