-->
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.  [ 2 posts ] 
Author Message
 Post subject: one-to-many foreign key issue
PostPosted: Wed Aug 14, 2013 5:41 pm 
Newbie

Joined: Tue Aug 13, 2013 4:59 pm
Posts: 2
Hello, i have this two tables in PostgreSQL:
Code:
CREATE TABLE COLLEGIO (
   codColl NUMERIC(4,0) NOT NULL,
   provincia VARCHAR(35) NOT NULL,
   /* Other fileds */
   PRIMARY KEY (codColl, provincia)
);
CREATE TABLE ELETTORE (
   codEl VARCHAR(16) NOT NULL,
   /* Other fileds */
   PRIMARY KEY (codEl),
   FOREIGN KEY (codColl, provincia) REFERENCES COLLEGIO (codColl, provincia)
);

I have created the classes with these XML files:
Code:
<class name="Collegio" table="COLLEGIO">
    <composite-id name="collegioKey" class="entities.keys.CollegioKey">
           <key-property type="int" name="codColl"/>
           <key-property type="string" name="provincia"/>
    </composite-id>
    <set name="elettori" table="ELETTORE" inverse="true" lazy="true" fetch="select">
        <key column="codel" not-null="true"/>
        <one-to-many class="entities.Elettore" />
    </set>
</class>
<class name="Elettore" table="ELETTORE">
    <id name="codEl" column="codEl">
        <generator class="assigned" />
    </id>

    <many-to-one name="collegio" class="entities.Collegio" fetch="select">
        <column name="codcoll" not-null="true"/>
        <column name="provincia" not-null="true"/>
    </many-to-one>
</class>

But when i try to run the project (for example for listing the content of the table "COLLEGIO") hibrenate give me this error:
Code:
org.hibernate.MappingException: Foreign key (FK_da1lkg53is2jg7wmcddlarf6s:ELETTORE [codel])) must have same number of columns as the referenced primary key (COLLEGIO [codColl,provincia])


What I have done wrong?


Top
 Profile  
 
 Post subject: Re: one-to-many foreign key issue
PostPosted: Tue Aug 20, 2013 6:31 am 
Beginner
Beginner

Joined: Wed Feb 06, 2013 2:43 am
Posts: 46
You need to have these two columns(codColl, provincia) also in your table ELETTORE to make use of foreign key.

CREATE TABLE ELETTORE (
codEl VARCHAR(16) NOT NULL,
codColl NUMERIC(4,0) NOT NULL,
provincia VARCHAR(35) NOT NULL,
/* Other fileds */
PRIMARY KEY (codEl),
FOREIGN KEY (codColl, provincia) REFERENCES COLLEGIO (codColl, provincia)
);

And then the mapping as :

<composite-id name="id" class="ELETTORE">
<key-property name="codEl" column="codEl" type="int"/>
<key-many-to-one name="Collegio" class="entities.Collegio">
<column name="first_name"/>
<column name="last_name"/>
</key-many-to-one>
</composite-id>

_________________
Thanks,
Ajit Singh
ajits@mindfiresolutions.com
www.mindfiresolutions.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.