aspose file tools*
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
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate composite-id" Watch "Hibernate composite-id" New topic
Author

Hibernate composite-id

thiago varella
Greenhorn

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"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<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>
<id name="idQuestion" type="int" unsaved-value="null" >
<column name="ID_QUESTION" sql-type="int(8)" not-null="true"/>
<generator class="assigned"/>
</id>
<id name="idResponse" type="int" unsaved-value="null" >
<column name="ID_RESPONSE" sql-type="int(8)" not-null="true"/>
<generator class="assigned"/>
</id> -->
<composite-id>
<key-property name="idMember">
<key-property name="idQuestion">
<key-property name="idResponse">
</composite-id>
<property name="text">
<column name="DS_TEXT" sql-type="varchar(1000)" not-null="true"/>
</property>
<property name="entryDate">
<column name="DT_ENTRY_DATE " sql-type="date" not-null="false"/>
</property>
<property name="visible">
<column name="FL_VISIBLE" sql-type="varchar(1)" not-null="false"/>
</property>
<property name="country">
<column name="NM_COUNTRY" sql-type="varchar(2)" not-null="false"/>
</property>
<property name="accepted">
<column name="FL_ACCEPTED" sql-type="varchar(1)" not-null="false"/>
</property>
</class>
</hibernate-mapping>
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
Greenhorn

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 net.sf.hibernate.id.IdentifierGenerator 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
Greenhorn

Joined: Jan 02, 2006
Posts: 1
Hi,
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"/>
</composite-id>

<property name="name"/>

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

</class>

In this case, lineId is an object mapped separately?


Thanks
Dirceu Semighini Filho


Thanks <br />Dirceu Semighini Filho
 
 
subject: Hibernate composite-id