-->
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.  [ 1 post ] 
Author Message
 Post subject: How-to configure mapping with composite key ?
PostPosted: Mon Nov 14, 2005 11:38 am 
Newbie

Joined: Tue Apr 26, 2005 2:05 am
Posts: 1
Oracle Database 10g Enterprise Edition Release 10.1.0.3.0
Hibernate version:3.0.5

Can't create mapping for tables:

CREATE TABLE USER
( "DCODE" NUMBER NOT NULL ENABLE,
"ECODE" NUMBER NOT NULL ENABLE,
"MCODE" VARCHAR2(10) NOT NULL ENABLE,
"UNAME" VARCHAR2(100),
CONSTRAINT "PK_USER" PRIMARY KEY ("DCODE", "ECODE", "MCODE")

CONSTRAINT FK_USER_OFFICE FOREIGN KEY ("DCODE", "MCODE")
REFERENCES OFFICE ("CODE", "MCODE") ENABLE

CREATE TABLE OFFICE
( "CODE" NUMBER NOT NULL ENABLE,
"MCODE" VARCHAR2(10) NOT NULL ENABLE,
CONSTRAINT "PK_OFFICE" PRIMARY KEY ("CODE", "MCODE")

Use MyEclipse 4.0, generate:

OFFICE.hbm.xml
-------------------------------------------------------------
<hibernate-mapping package="......">

<class name="OFFICE" table="OFFICE">
<composite-id name="id" class="OfficeKey">
<key-property name="code" column="CODE" type="java.lang.Long"/>
<key-property name="mcode" column="MCODE" type="java.lang.String"/>
</composite-id>

<set name="UserSet" inverse="true">
<key column="DCODE"/>
<one-to-many class="USER"/>
</set>

<set name="UserSet1" inverse="true">
<key column="MCODE"/>
<one-to-many class="USER"/>
</set>
</class>

</hibernate-mapping>
-------------------------------------------------------------

and

USER.hbm.xml
-------------------------------------------------------------
<hibernate-mapping package="....">

<class name="USER" table="USER">
<composite-id name="id" class="UserKey">
<key-many-to-one name="office" column="DCODE" class="OFFICE"/>
<key-property name="ecode" column="ECODE" type="java.lang.Long"/>
<key-many-to-one name="office1" column="MCODE" class="OFFICE"/>
</composite-id>
........
</hibernate-mapping>
-------------------------------------------------------------

i change USER.hbm.xml to :

-------------------------------------------------------------
<hibernate-mapping package="....">

<class name="USER" table="USER">
<composite-id name="id" class="UserKey">
<key-many-to-one name="OFFICE" class="OFFICE">
<column name="CODE"/>
<column name="MCODE"/>
</key-many-to-one>
<key-property name="ecode" column="ECODE" type="java.lang.Long"/>
</composite-id>
-------------------------------------------------------------

error:
%%%% Error Creating SessionFactory %%%%
org.hibernate.MappingException: Foreign key (FKF8AB675E31EB7FDF:USER [MCODE])) must have same number of columns as the referenced primary key (OFFICE [CODE,MCODE])
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)


After read FAQ, try change USER.hbm.xml :
------------------------------------------------------------------
<hibernate-mapping package=".....">

<class name="USER" table="USER">
<composite-id name="id" class="UserKey">
<key-property name="DCODE" column="DCODE" type="java.lang.Long"/>
<key-property name="MCODE" column="MCODE" type="java.lang.String"/>
<key-property name="ECODE" column="ECODE" type="java.lang.Long"/>
</composite-id>

<many-to-one name="office" class="OFFICE" insert="false" update="false">
<column name="CODE"/>
<column name="MCODE"/>
</many-to-one>
........
</hibernate-mapping>
--------------------------------------------------------


%%%% Error Creating SessionFactory %%%%
org.hibernate.MappingException: Foreign key (FKF8AB675E31EB7FDF:USER [MCODE])) must have same number of columns as the referenced primary key (OFFICE [CODE,MCODE])
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)

How-to configure mapping many-to-one relation with composite key ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.