-->
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.  [ 1 post ] 
Author Message
 Post subject: Eagerly load polymorphic associations using Linq & NH
PostPosted: Tue Apr 13, 2010 6:39 am 
Newbie

Joined: Wed Oct 12, 2005 8:20 am
Posts: 1
Location: Belgrade
Hi,

Does anybody know is it possible to eagerly load polymorphic association using Linq and NH?

For example: Company is base class, Department is inherited from Company, and Company has association Employees to the User (one-to-many) and also association to the Country (many-to-one).
Here is mapping part related to inherited class (without User and Country classes):

Code:
   <class name="Company" discriminator-value="Company">
      <id name="Id" type="int" unsaved-value="0" access="nosetter.camelcase-underscore">
         <generator class="native"></generator>
      </id>
      <discriminator column="OrganizationUnit" type="string" length="10" not-null="true"/>
      <property name="Name" type="string" length="50" not-null="true"/>
      <many-to-one name="Country" class="Country" column="CountryId" not-null ="false" foreign-key="FK_Company_CountryId" access="field.camelcase-underscore" />
      <set name="Departments" inverse="true" lazy="true" access="field.camelcase-underscore">
         <key column="DepartmentParentId" not-null="false" foreign-key="FK_Department_DepartmentParentId"></key>
         <one-to-many class="Department"></one-to-many>
      </set>
      <set name="Employees" inverse="true" lazy="true" access="field.camelcase-underscore">
         <key column="CompanyId" not-null="false" foreign-key="FK_User_CompanyId"></key>
         <one-to-many class="User"></one-to-many>
      </set>
      <subclass name="Department" extends="Company" discriminator-value="Department">
         <many-to-one name="DepartmentParent" class="Company" column="DepartmentParentId" not-null ="false" foreign-key="FK_Department_DepartmentParentId" access="field.camelcase-underscore" />
      </subclass>
   </class>


I do not have problem to eagerly load any of the association on the Company:
Code:
Session.Query<Company>().Where(c => c.Name == "Main Company").Fetch(c => c.Country).Single();
Session.Query<Company>().Where(c => c.Name == "Main Company").FetchMany(c => c.Employees).Single();


Also, I could eagerly load not polymorphic association on the department:
Code:
Session.Query<Department>().Where(d => d.Name == "Department 1").Fetch(d => d.DepartmentParent).Single();


But I get NullReferenceException when I try to eagerly load any of the polymorphic association (from the Department):
Code:
Assert.Throws<NullReferenceException>(() => Session.Query<Department>().Where(d => d.Name == "Department 1").Fetch(d => d.Country).Single());
Assert.Throws<NullReferenceException>(() => Session.Query<Department>().Where(d => d.Name == "Department 1").FetchMany(d => d.Employees).Single());


Am I doing something wrong or this is not supported yet?
Any help is appreciated!
Voislav


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

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.