• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate composite-id

 
thiago varella
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chris Mathews
Ranch Hand
Posts: 2712
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirceu Semighini Filho
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic