File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Hibernate composite-id Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate composite-id" Watch "Hibernate composite-id" New topic

Hibernate composite-id

thiago varella

Joined: Sep 23, 2003
Posts: 6
Hi guys!
I�d like to know how to do composite-id with hibernate. I have a simple table and I�ve tried to do this:
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
<class name="persistencia.Response" table="RESPONSE">

<!-- <id name="idMember" type="int" unsaved-value="null" >
<column name="ID_MEMBER" sql-type="int(8)" not-null="true"/>
<generator class="assigned"/>
<id name="idQuestion" type="int" unsaved-value="null" >
<column name="ID_QUESTION" sql-type="int(8)" not-null="true"/>
<generator class="assigned"/>
<id name="idResponse" type="int" unsaved-value="null" >
<column name="ID_RESPONSE" sql-type="int(8)" not-null="true"/>
<generator class="assigned"/>
</id> -->
<key-property name="idMember">
<key-property name="idQuestion">
<key-property name="idResponse">
<property name="text">
<column name="DS_TEXT" sql-type="varchar(1000)" not-null="true"/>
<property name="entryDate">
<column name="DT_ENTRY_DATE " sql-type="date" not-null="false"/>
<property name="visible">
<column name="FL_VISIBLE" sql-type="varchar(1)" not-null="false"/>
<property name="country">
<column name="NM_COUNTRY" sql-type="varchar(2)" not-null="false"/>
<property name="accepted">
<column name="FL_ACCEPTED" sql-type="varchar(1)" not-null="false"/>
Please someone help me!
Thanks for help,
Thiago Varella

Simple Java Programmer
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Your best source of information is the Hibernate documentation. See this section on composite-id and Components as Composite Identifiers. Unfortunately, you cannot use an IdentifierGenerator with composite ids so your application will need to explicitly assign the ids. Note that is is strongly recommended that you avoid composite ids whenever possible...
jalal ud-din

Joined: Nov 04, 2004
Posts: 4
Well in fact you _can_ use generated ids for acheiving the same end.

Instead of using composite ids (which is something of a performance in Hibernate), I found it simpler to write my own subclass.

That tiny code generated a unique identifier for a class based on a number of the class' properties.

The generation algorithm can be as simple as string concetanation or the md5 hash of larger combinations of data.

.increase the peace
Dirceu Semighini Filho

Joined: Jan 02, 2006
Posts: 1
I am having the same problem as Thiago.

I have read a lot of things about it, and I found that is something on Hibernate documentation (8 Components as composite identifiers) which probably solve this problem. But they don't explain how to do it.
In their example they do this mapping:
<class name="OrderLine">

<composite-id name="id" class="OrderLineId">
<key-property name="lineId"/>
<key-property name="orderId"/>
<key-property name="customerId"/>

<property name="name"/>

<many-to-one name="order" class="Order"
insert="false" update="false">
<column name="orderId"/>
<column name="customerId"/>


In this case, lineId is an object mapped separately?

Dirceu Semighini Filho

Thanks <br />Dirceu Semighini Filho
I agree. Here's the link:
subject: Hibernate composite-id
jQuery in Action, 3rd edition