-->
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: Polymorphic Relationships
PostPosted: Thu Sep 25, 2008 3:51 pm 
Newbie

Joined: Mon May 05, 2008 10:34 am
Posts: 11
Location: Houston, TX
This is more of a question than a problem. I have a common_code table that I have a parent_id field on which is recursive to the table. NHibernate does not seem to put criteria for my children objects. I guess what I'm asking for is if NHibernate allows me to have Polymorphic relationships. Basically ParentId relates to different derivatives of CommonCode based on which class you are starting from.

Hibernate version:2.0.0.3001

Tables
CREATE TABLE common_code
(
common_code_id INT NOT NULL IDENTITY PRIMARY KEY,
common_code_type_id INT NOT NULL REFERENCES common_code_type(common_code_type_id),
parent_id INT NULL REFERENCES common_code(common_code_id),
common_code_name VARCHAR(100) NOT NULL,
commmon_code_descr VARCHAR(100) NULL,
common_code_value VARCHAR(100) NULL
)
GO

Mapping documents:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="CompanyName.Portal.Domain.CommonCode,CompanyName.Portal.Domain" table="common_code" lazy="true">
<!-- Common Properties Accross CommonCode -->
<id name="Id" column="common_code_id" type="int">
<generator class="native" />
</id>
<discriminator column="common_code_type_id" />
<property name="Name" column="common_code_name" type="string" not-null="true" />

<!--
*************** Concrete Implementations of CommonCode ****************
-->

<!-- App class -->
<subclass name="CompanyName.Portal.Domain.App, CompanyName.Portal.Domain" discriminator-value="4">
<property name="Description" column="commmon_code_descr" type="string" not-null="true" />
<property name="RelativePath" column="common_code_value" type="string" not-null="true" />
<bag name="Features" inverse="true" lazy="true" cascade="delete">
<key column="parent_id" />
<one-to-many class="CompanyName.Portal.Domain.AppFeature,CompanyName.Portal.Domain" />
</bag>
</subclass>

<!-- AppFeature -->
<subclass name="CompanyName.Portal.Domain.AppFeature, CompanyName.Portal.Domain" discriminator-value="5">
<many-to-one name="Application" column="parent_id" cascade="save-update" not-null="true" />
<property name="Description" column="commmon_code_descr" type="string" not-null="true" />
<property name="RelativePath" column="common_code_value" type="string" not-null="true" />
<bag name="AppRoleFeatureAccess" inverse="true" lazy="true" cascade="delete">
<key column="app_feature_id" />
<one-to-many class="CompanyName.Portal.Domain.AppRoleFeatureAccess,CompanyName.Portal.Domain" />
</bag>
</subclass>

<!-- AppRole -->
<subclass name="CompanyName.Portal.Domain.AppRole, CompanyName.Portal.Domain" discriminator-value="6">
<many-to-one name="Application" column="parent_id" cascade="save-update" not-null="true" />
<property name="Description" column="commmon_code_descr" type="string" not-null="true" />
<bag name="FeatureAccess" inverse="true" lazy="true" cascade="delete">
<key column="app_role_id" />
<one-to-many class="CompanyName.Portal.Domain.AppRoleFeatureAccess,CompanyName.Portal.Domain" />
</bag>
<bag name="RoleMembers" inverse="true" lazy="true" cascade="delete">
<key column="app_role_id" />
<one-to-many class="CompanyName.Portal.Domain.AppRoleMember,CompanyName.Portal.Domain" />
</bag>
</subclass>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs: No exceptions

Name and version of the database you are using: SQL Server 2005

The generated SQL (show_sql=true):
2008-09-25 14:33:57,294 [5] INFO NHibernate.Loader.Loader [(null)] - SELECT this_.common_code_id as common1_4_0_, this_.common_code_name as common3_4_0_, this_.commmon_code_descr as commmon4_4_0_, this_.common_code_value as common5_4_0_ FROM common_code this_ WHERE this_.common_code_type_id='4'
2008-09-25 14:33:57,294 [5] INFO NHibernate.Loader.Loader [(null)] - SELECT features0_.parent_id as parent6_1_, features0_.common_code_id as common1_1_, features0_.common_code_id as common1_4_0_, features0_.common_code_name as common3_4_0_, features0_.parent_id as parent6_4_0_, features0_.commmon_code_descr as commmon4_4_0_, features0_.common_code_value as common5_4_0_ FROM common_code features0_ WHERE features0_.parent_id=@p0
2008-09-25 14:33:57,294 [5] INFO NHibernate.Loader.Loader [(null)] - SELECT features0_.parent_id as parent6_1_, features0_.common_code_id as common1_1_, features0_.common_code_id as common1_4_0_, features0_.common_code_name as common3_4_0_, features0_.parent_id as parent6_4_0_, features0_.commmon_code_descr as commmon4_4_0_, features0_.common_code_value as common5_4_0_ FROM common_code features0_ WHERE features0_.parent_id=@p0


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.