• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate composite-id

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2713
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Being a smart alec beats the alternative. This tiny ad knows what I'm talking about:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic