-->
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: Composite-Id and recursive relation / Parent-Child relation
PostPosted: Tue Mar 29, 2011 10:57 am 
Newbie

Joined: Tue Mar 29, 2011 10:23 am
Posts: 1
Hi,

i have the following scenario (For better illustration, the example is very simplified)
Image

BAS_COSTCODE refers to BAS_CONTEXT. The key for this table is a composite with COSTCODEID and the CONTEXT_FK. CONTEXT_FK refers to BAS_CONTEXT.
To build up a hierarchical tree, a CostCode can have a parent. For this reason there is a reference to the table itselfe.

The schema file for Context is like that:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   schema="dbo"
                   assembly="App.NHibernate.DataObjects"
                   namespace="App.NHibernate.DataObjects">

  <class name="App.NHibernate.DataObjects.Context" table="BAS_CONTEXT" lazy="false" mutable="true" >

    <id name="Id" type="Int16" column="CONTEXTID" >
      <generator class="assigned"/>
    </id>

    <property name="Name" column="NAME" type="String" not-null="true"/>
   </class>

</hibernate-mapping>

And the schema for CostCode is like that:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="dbo" assembly="App.NHibernate.DataObjects" namespace="App.NHibernate.DataObjects">

  <class name="App.NHibernate.DataObjects.CostCode" table="CON_COSTCODE" mutable="true" >

    <composite-id name="CompositeId" class="CostCodeId" unsaved-value="any">
      <key-property name="Id" column="COSTCODEID" type="String" />
      <key-many-to-one name="Context" column="CONTEXT_FK" class="Context" lazy="proxy"/>
    </composite-id>


    <many-to-one name="ParentCostCode" class="CostCode" lazy="proxy" insert="true" update="true" >
      <column name="PARENTCOSTCODE_FK"/>
      <column name="CONTEXT_FK"/>
    </many-to-one>

    <property name="Name" column="NAME" type="String"/>
  </class>

</hibernate-mapping>


If i create a new CostCode entity and run Commit() i get the following exception:
System.IndexOutOfRangeException: "Invalid index 13 for this SqlParameterCollection with Count=13."

I think NHibernate has a problem with the reference ParentCostCode, which refers to a parent CostCode object.
NHIbernate assumes to write a value to column PARENTCOSTCODE_FK and to CONTEXT_FK. In the mapping the composite-id points to CONTEXT_FK and the ParentCostCode points to CONTEXT_FK. So they are "sharing" the same column and NHibernate should only write a value to the column PARENTCOSTCODE_FK.

One solution is to add an additional column (maybe PARENTCONTEXT_FK) to table BAS_COSTCODE to represent the Context for the parent object.
But i don't want to have an additional column, because the values of CONTEXT_FK and PARENTCONTEXT_FK must have the same value, if there exist a referenceto a parent object. And that would be redundant.

Are there better solutions for that kind of problem? Or i can't prevent a additional column in BAS_COSTCODE?

Many thanks in advance for your answers!


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.