-->
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: org.hibernate.MappingException: Foreign Key
PostPosted: Thu Oct 06, 2005 6:15 pm 
Newbie

Joined: Wed Jul 06, 2005 7:37 pm
Posts: 4
Im getting an exception after trying to test my hibernate mapping. The tables looks something like this:

TABLE a
a
b
primary key (a, b)
foreign key (b) on (c.a)

TABLE b
a
b
c
primary key (a)
foreign key (b, c) on (a.a, a.b)

TABLE c
a
b
c
primary key(a)

Table A's mapping:
Code:
    <class name="ProductType" table="PRODUCT_TYPES">
        <composite-id name="id" class="ProductTypeKey">
            <key-many-to-one name="product" column="PTYP_PRODUCT_CODE" class="Product"/>
            <key-property name="ptypProductType" column="PTYP_PRODUCT_TYPE" type="java.lang.String"/>
        </composite-id>
.....



Table B's mapping looks something like:
Code:
    <class name="PartnerAgreement" table="PARTNER_AGREEMENTS">
        <id name="agreAgreementNumber" column="AGRE_AGREEMENT_NUMBER" type="java.lang.Integer">
            <generator class="native"/>
        </id>

        <many-to-one name="productType" column="AGRE_PRODUCT_CODE" class="ProductType"  not-null="true" />

        <many-to-one name="productType1" column="AGRE_PRODUCT_TYPE" class="ProductType"  not-null="true" />



How do I map AGRE_PRODUCT_CODE and AGRE_PRODUCT_TYPE for table B which are the primary composite key from table a? Is what I've done correct. Im getting the exception below.

Hibernate version: 3.0

Full stack trace of any exception that occurs:

Code:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [spring-context.xml]: Initialization of bean failed; nested exception is org.hibernate.MappingException: Foreign key (FKA0044FA0EE76CD:PARTNER_AGREEMENTS [AGRE_PRODUCT_TYPE])) must have same number of columns as the referenced primary key (PRODUCT_TYPES [PTYP_PRODUCT_CODE,PTYP_PRODUCT_TYPE])
org.hibernate.MappingException: Foreign key (FKA0044FA0EE76CD:PARTNER_AGREEMENTS [AGRE_PRODUCT_TYPE])) must have same number of columns as the referenced primary key (PRODUCT_TYPES [PTYP_PRODUCT_CODE,PTYP_PRODUCT_TYPE])
   at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:86)
   at org.hibernate.mapping.ForeignKey.setReferencedTable(ForeignKey.java:51)
   at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:976)
   at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:921)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:999)
   at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:767)
   at org.springframework.orm.hibernate3.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:693)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1003)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:348)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:226)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:147)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:269)
   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:317)
   at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:80)
   at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:65)
   at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:56)
   at tuinsurance.lib.integration.dao.impl.HibRateTripFactorTest.setUp(HibRateTripFactorTest.java:21)
   at junit.framework.TestCase.runBare(TestCase.java:125)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)



Name and version of the database you are using: Oracle 9i


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 11:16 pm 
Newbie

Joined: Wed Jul 06, 2005 7:37 pm
Posts: 4
The mapping of the composite keys is wrong. The mapping of:

<many-to-one name="productType" column="AGRE_PRODUCT_CODE" class="ProductType" not-null="true" />

<many-to-one name="productType1" column="AGRE_PRODUCT_TYPE" class="ProductType" not-null="true" />

should be

<many-to-one name="productType" class="ProductType" not-null="true" >
<column name="AGRE_PRODUCT_CODE"/>
<column name="AGRE_PRODUCT_TYPE"/>
</many-to-one>


That fixed the problem.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.