-->
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.  [ 8 posts ] 
Author Message
 Post subject: HELP: Mapping composite primary key
PostPosted: Thu Jul 22, 2004 6:21 am 
Beginner
Beginner

Joined: Sun Jun 13, 2004 9:49 pm
Posts: 38
I'm getting quite stuck with this. I need to create a mapping for a very simple table with a composite primary key. Every way i have tried has given me errors. Here is the table.
Code:
create table z@ifmpsgl.ifmpsglx
(
   m_admin_div    char(10) not null,
   m_finance_div    char(10) not null,
   m_unit          char(10) not null,
   m_nature       char(10) not null,
   p_business_unit char(5)  not null,
   p_account       char(10),
   p_dept          char(10),
   p_project       char(15),
   p_product       char(6),
   p_affiliate    char(5),
   constraint z@ifmpsgl.ifmpsglx_pk primary key
   (
      m_admin_div,
      m_finance_div,
      m_unit,
      m_nature
   )      
);

Could someone provide an example hbm.xml file for this. I'm trying to justify dumping our in-house mapping system (written by me) for Hibernate. So far i'm failing.

Please help.

Darren.


Top
 Profile  
 
 Post subject: OOPS: The mapping file and generated sql:
PostPosted: Thu Jul 22, 2004 6:28 am 
Beginner
Beginner

Joined: Sun Jun 13, 2004 9:49 pm
Posts: 38
Mapping:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
   <class name="com.dolby.hibernate.test.XRMapping" table="ifmpsglx" schema="z@ifmpsgl">
   
      <composite-id>
         <key-property name="mapicsAdminDivision"/>
         <key-property name="mapicsFinanceDivision"/>
         <key-property name="mapicsUnit"/>
         <key-property name="mapicsNature"/>
      </composite-id>

      <property name="mapicsAdminDivision" column="m_admin_div" type="string" length="10" not-null="true"/>
      <property name="mapicsFinanceDivision" column="m_finance_div" type="string" length="10" not-null="true"/>
      <property name="mapicsUnit" column="m_unit" type="string" length="10" not-null="true"/>
      <property name="mapicsNature" column="m_nature" type="string" length="10" not-null="true"/>
      <property name="peoplesoftBusinessUnit" column="p_business_unit" type="string" length="5" not-null="true"/>
      <property name="peoplesoftAccount" column="p_account" type="string" length="10" not-null="false"/>
      <property name="peoplesoftDepartmentId" column="p_dept" type="string" length="10" not-null="false"/>
      <property name="peoplesoftProjectId" column="p_project" type="string" length="15" not-null="false"/>
      <property name="peoplesoftProductId" column="p_product" type="string" length="6" not-null="false"/>
      <property name="peoplesoftAffiliateId" column="p_affiliate" type="string" length="5" not-null="false"/>
   </class>
</hibernate-mapping>

SQL:
Code:
select
   xrmapping0_.mapicsAdminDivision as x0_0_,
   xrmapping0_.mapicsFinanceDivision as x0_1_,
   xrmapping0_.mapicsUnit as x0_2_,
   xrmapping0_.mapicsNature as x0_3_
from
   z@ifmpsgl.ifmpsglx xrmapping0_


I MUST be doing something wrong.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 22, 2004 6:36 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you're telling us the error you get....

don't forget equals & hashcode...

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 22, 2004 7:02 am 
Beginner
Beginner

Joined: Sun Jun 13, 2004 9:49 pm
Posts: 38
But, look at the columns being selected in the sql statement. It's trying to select the attribute names in the class and not the column names. Therefore i'm getting a JDBC error.

This is very strange.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 22, 2004 7:24 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
<composite-id>
<key-property name="mapicsAdminDivision"/>
<key-property name="mapicsFinanceDivision"/>
<key-property name="mapicsUnit"/>
<key-property name="mapicsNature"/>
</composite-id>


your forgot to specify column attribute, doing that, it is wanted that the column names are the same as the attributes names...

so just add the colunm name, and of course you don't need to duplicate a property when it is part of a composite-id: so also remove
Code:
     <property name="mapicsAdminDivision" column="m_admin_div" type="string" length="10" not-null="true"/>
      <property name="mapicsFinanceDivision" column="m_finance_div" type="string" length="10" not-null="true"/>
      <property name="mapicsUnit" column="m_unit" type="string" length="10" not-null="true"/>
      <property name="mapicsNature" column="m_nature" type="string" length="10" not-null="true"/>

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 22, 2004 7:45 am 
Beginner
Beginner

Joined: Sun Jun 13, 2004 9:49 pm
Posts: 38
Hehe,

I knew i must have been doing something wrong.
Thanks Anthony, that solved it.

I was making the assumption that you just referenced which columns were in the composite key, but still declared all columns as properties.

I must get our in-house system out of my head :)

Thanks again.

Darren.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 22, 2004 7:47 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you're welcome

one thing about composite id, don't forget to code equals & hashcode and do it well. It is extremely important

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 22, 2004 8:55 am 
Beginner
Beginner

Joined: Sun Jun 13, 2004 9:49 pm
Posts: 38
Ahh i see commons.lang a calling. lol


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