CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Feb 2001
    Posts
    872

    NHibernate - Child-Parent relationship -- Key happens to be a composite key

    Help! Child-Parent relationship -- Key happens to be a composite key

    Hi, I'm having a lot of trouble trying to get this work. I have two classes:
    1. AppProperty (Parent)
    2. AppPropertyValue (Child)

    1. AppProperty class' PK column is PropertyUIN (Int32).
    2. AppPropertyValue class' composite key column is {PropertyUIN (Int32), PropertyValue (String)}
    3. One AppProperty instance can relate to MANY AppPropertyValue instance. The two are related by "PropertyUIN".

    Trouble is, when I tried to persist the child class, I get:

    SqlException wrapped around by a NHibernate.ADOException :

    Code:
    Message	"Column name 'PropertyUIN' appears more than once in the result column list.
    Statement(s) could not be prepared."	String
    "PropertyUIN" appears twice because it's part of the composite key AND at the same time it appears for the second time in AppPropertyValue's hbm file for I need to declare "many-to-one" between AppProperty and AppPropertyValue. What should I do to resolve this situation?

    Thanks in advance.


    Code Fragment:
    Code:
    Public Class AppProperty
    		Implements ISerializable
    
    		Protected _propertyUIN As Int32
    		...
    		...
    		Public Property PropertyUIN() As Int32
    			Get
    				Return _propertyUIN
    			End Get
    			Set(ByVal Value As Int32)
    				_propertyUIN = Value
    			End Set
    		End Property
    		...
    End Class
    
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
    	<class name="application.utilities.settings.AppProperty, Utility" table="AppProperty">
    		<id name="PropertyUIN" column="PropertyUIN" type="Int32" > 
    			<generator class="identity" /> 
    		</id> 
    		... stuff ...
    		<set name="Values" inverse="true" cascade="all" table="AppPropertyValue">
    			<key column="PropertyUIN"/>
    			<one-to-many class="application.utilities.settings.AppPropertyValue, Utility"/>
    		</set>
    		... the rest of it ...
    	</class>
    </hibernate-mapping>
    
    
    Public Class AppPropertyValue
    		Implements ISerializable
    
    		Protected _propertyUIN As Int32
    		Protected _propertyValue As String
    
    		Protected _parentProperty As AppProperty
    		...
    		...
    		Public Property PropertyUIN() As Int32
    			Get
    				Return _propertyUIN
    			End Get
    			Set(ByVal Value As Int32)
    				_propertyUIN = Value
    			End Set
    		End Property
    
    		Public Property PropertyValue() As String
    			Get
    				Return _propertyValue
    			End Get
    			Set(ByVal Value As String)
    				_propertyValue = Value
    			End Set
    		End Property
    
    		Public Property ParentProperty() As AppProperty
    			Get
    				Return _parentProperty
    			End Get
    			Set(ByVal Value As AppProperty)
    				_parentProperty = Value
    			End Set
    		End Property
    		...
    End Class
    
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
    	<class name="application.utilities.settings.AppPropertyValue, Utility" table="AppPropertyValue">
    		<composite-id>
    			<key-property name="PropertyUIN" column="PropertyUIN" type="Int32"/>
    			<key-property name="PropertyValue" column= "PropertyValue" type="String" length="2500"/>
    		</composite-id>
    		<many-to-one name="ParentProperty" column="PropertyUIN" class="application.utilities.settings.AppProperty, Utility" />
    		... stuff ...
    	</class>
    </hibernate-mapping>
    
    CREATE TABLE AppProperty (
    	PropertyUIN INTEGER IDENTITY(1,1),
    	...
    	PRIMARY KEY  (PropertyUIN)
    )
    
    CREATE TABLE AppPropertyValue (
    	PropertyUIN INTEGER,
    	PropertyValue VARCHAR(2500),
    	...
    )
    Last edited by THY02K; March 30th, 2005 at 08:38 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured