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: HowTo map <many-to-one> with 2 element primary key?
PostPosted: Sun Dec 28, 2008 8:09 pm 
Newbie

Joined: Mon Dec 22, 2008 6:32 pm
Posts: 5
Hello!

Again I came across something strange.

I have a Class with two elements as primary key and

Code:
public class Class1
    {
        private int _ID1;
        private int _ID2;
        private Class1 _NextClass1Object;
        private Class1 _PrevClass1Object;

        public virtual int ID1
        {
            get { return _ID1; }
            set { _ID1 = value; }
        }
       
        public virtual int ID2
        {
            get { return _ID2; }
            set { _ID2 = value; }
        }

        public virtual Class1 NextClass1Object
        {
            get { return _NextClass1Object; }
            set { _NextClass1Object = value; }
        }

        public virtual Class1 PrevClass1Object
        {
            get { return _PrevClass1Object; }
            set { _PrevClass1Object = value; }
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }


I mapped this class in this way:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" namespace="HiberTest" assembly="HiberTest">
  <class name="HiberTest.Class1,HiberTest" table="class1" lazy="false">

    <composite-id>
      <key-property name="ID1"/>
      <key-property name="ID2"/>
    </composite-id>

    <many-to-one name="NextClass1Object" class="Class1" column="NextClass1Object"/>
    <many-to-one name="PrevClass1Object" class="Class1" column="PrevClass1Object"/>
  </class>

</hibernate-mapping>


With this mapping I get an error:
Quote:
Foreign key (FK1521A76764F2E76E:class1 [NextClass1Object])) must have same number of columns as the referenced primary key (class1 [ID1, ID2])


Does someone of you know how I can fix this error?

I think I have to put somehow a two element key in one column (obviously not possible).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2008 3:10 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can specify more columns:

Code:
<many-to-one name="NextClass1Object" class="Class1">
    <column name="NextClass1ObjectID1"/>
    <column name="NextClass1ObjectID2"/>
</many-to-one>

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2008 12:30 pm 
Newbie

Joined: Mon Dec 22, 2008 6:32 pm
Posts: 5
Hi Wolli,

thanks! This was the solution. Realy Great. :-D

Now in combination with my other post here: http://forum.hibernate.org/viewtopic.php?t=993312 it is possible to inherit from a List and give every member of this list a link to it predecessor and successor.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2008 12:35 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Quote:
Now in combination with my other post here: http://forum.hibernate.org/viewtopic.php?t=993312 it is possible to inherit from a List and give every member of this list a link to it predecessor and successor.


Is this a question ? As mentioned in the other post, you can't directly inherit from a list. You have to wrap it !

_________________
--Wolfgang


Top
 Profile  
 
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.