-->
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.  [ 7 posts ] 
Author Message
 Post subject: org.hibernate.MappingException: Foreign key must have ...
PostPosted: Thu Mar 16, 2006 1:30 pm 
Newbie

Joined: Thu Mar 16, 2006 12:06 pm
Posts: 6
I know that there are a lot of threads with the same question, but I tried all seggested solutions and none worked for me...
Here are part of my mapping (generated with hbm2hbmxml)

Code:
<hibernate-mapping>
    <class name="OfferteFornitori" table="offerte_fornitori">
        <composite-id name="id" class="OfferteFornitoriId">
            <key-property name="fkFornitori" type="short">
                <column name="fk_fornitori" />
            </key-property>
            <key-property name="fkRdo" type="int">
                <column name="fk_rdo" />
            </key-property>
        </composite-id>
        <many-to-one name="rdo" class="Rdo" update="false" insert="false" fetch="select">
            <column name="fk_rdo" not-null="true" />
        </many-to-one>
        <many-to-one name="fornitori" class="Fornitori" update="false" insert="false" fetch="select">
            <column name="fk_fornitori" not-null="true" />
        </many-to-one>
        <property name="codice" type="string">
            <column name="codice" length="65535" />
        </property>
    </class>

    <class name="ArticoliOfferti" table="articoli_offerti">
        <composite-id name="id" class="ArticoliOffertiId">
            <key-property name="fkOfferteFornitoriFor" type="short">
                <column name="fk_offerte_fornitori_for" />
            </key-property>
            <key-property name="fkOfferteFornitoriRdo" type="int">
                <column name="fk_offerte_fornitori_rdo" />
            </key-property>
            <key-property name="fkArticoli" type="int">
                <column name="fk_articoli" />
            </key-property>
        </composite-id>
        <many-to-one name="articoli" class="Articoli" update="false" insert="false" fetch="select">
            <column name="fk_articoli" not-null="true" />
        </many-to-one>
        <many-to-one name="offerteFornitoriByFkOfferteFornitoriFor" class="OfferteFornitori" insert="false" update="false" fetch="select">
            <column name="fk_offerte_fornitori_for" not-null="true" />
        </many-to-one>
        <many-to-one name="offerteFornitoriByFkOfferteFornitoriRdo" class="OfferteFornitori" update="false" insert="false" fetch="select">
            <column name="fk_offerte_fornitori_rdo" not-null="true" />
        </many-to-one>
       
        <property name="prezzo" type="double">
            <column name="prezzo" precision="0" scale="0" not-null="true" />
        </property>
    </class>
</hibernate-mapping>



Trying to generate the java code with hbm2java I got:
Code:
org.hibernate.MappingException: Foreign key (FK405FC7E1E8A9975D:articoli_offerti [fk_offerte_fornitori_for])) must have same number of columns as the referenced primary key (offerte_fornitori [fk_fornitori,fk_rdo])


I tried using property-ref, putting key-many-to-one, but nothing works...

Any suggestion?

Thank you in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 16, 2006 9:35 pm 
Beginner
Beginner

Joined: Wed Nov 30, 2005 2:41 pm
Posts: 29
Since OfferteFornitori has a composite key, with 2 columns, the reference:

<many-to-one name="offerteFornitoriByFkOfferteFornitoriFor" class="OfferteFornitori" insert="false" update="false" fetch="select">
<column name="fk_offerte_fornitori_for" not-null="true" />
</many-to-one>

must also list 2 columns; hibernate needs 2 columns to build the composite key.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 3:25 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
If this is puretly generated by reverseengineering and hbm2hbmxml then please provide the DDL and the resulting hbm.xml in a jira case so i can look at why it is generating this bad form.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 3:44 am 
Newbie

Joined: Thu Mar 16, 2006 12:06 pm
Posts: 6
Dwightd wrote:
Since OfferteFornitori has a composite key, with 2 columns, the reference:

<many-to-one name="offerteFornitoriByFkOfferteFornitoriFor" class="OfferteFornitori" insert="false" update="false" fetch="select">
<column name="fk_offerte_fornitori_for" not-null="true" />
</many-to-one>

must also list 2 columns; hibernate needs 2 columns to build the composite key.


Nothing.
I changed:
Code:
<many-to-one name="offerteFornitoriByFkOfferteFornitoriFor" class="OfferteFornitori" insert="false" update="false" fetch="select">
            <column name="fk_offerte_fornitori_for" not-null="true" />
        </many-to-one>
        <many-to-one name="offerteFornitoriByFkOfferteFornitoriRdo" class="OfferteFornitori" update="false" insert="false" fetch="select">
            <column name="fk_offerte_fornitori_rdo" not-null="true" />
        </many-to-one>


to:
Code:
<many-to-one name="offerteFornitori" class="OfferteFornitori" insert="false" update="false" fetch="select">
            <column name="fk_offerte_fornitori_for" not-null="true" />
            <column name="fk_offerte_fornitori_rdo" not-null="true" />
        </many-to-one>

but I got the same exception


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 5:43 am 
Newbie

Joined: Thu Mar 16, 2006 12:06 pm
Posts: 6
Ok, I resolved.
There was also a set in the hbm of "OfferteFornitori" and I changed it to:
Code:
<set name="articoliOffertisForFkOfferteFornitori" inverse="true">
     <key>
         <column name="fk_offerte_fornitori_for" not-null="true" />
         <column name="fk_offerte_fornitori_rdo" not-null="true" />
    </key>
    <one-to-many class="ArticoliOfferti" />
</set>


But now I have another exception:

Code:
org.hibernate.tool.hbm2x.ExporterException: Error while processing template the second time


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 6:03 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
run with ant -debug to get the full exception.

(btw. this is known issue when using hbm2seam which does not currently support composite ids)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 6:38 am 
Newbie

Joined: Thu Mar 16, 2006 12:06 pm
Posts: 6
I'm sorry, really stupid problem... the output directory was not present.
Thank you a lot guys!

P.S. I posted the bug to jira


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