Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Composite id

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to use hibernate to fetch data from a database table which does'nt have a primary key defined. However the fact is 3 fields of that table together are unique in the table.

I am not able to figure out how to represent this in the hibernate mapping table for that class. I read that there is a <composite-primarykey..> tag to do this. Tried a few combinations but failed to fetch the entire data from the table.

Could someone please throw light on this and the way xxx.hbm.xml file, the classes should be defined in this sceneario.

Thanks
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried so far? If you post the mapping as far as you've managed we can take a look.
 
srija patel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I have hbm file like above.
My table ORDER does not have any primary key, but the fields combo of
productName, name, key is Unique.

When I fire this query, I get weird resultset which is not as expected.
So my colleague suggested me using Composite id.
But I don't know how to use that here.

Any help appreciated .........
[ December 22, 2008: Message edited by: Paul Sturrock ]
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the docs. These include an example.

Basically, rather than mapping your id with a native generator, use the composite-id element and wrap up the three properties that comprise your composite key. I'd also add a unique index in your database if there isn't already one (entities without PKs are dangerous)

The only other step you'll need to do is either provide a composite key class, or override equals() and hashcode() in your mapped class.
 
srija patel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this on Net.

<hibernate-mapping>
<class
name="org.example.composite.Application"
table="APPLICATION">

<!-- applicationPK is another class the implements Serializable -->

<composite-id
name="applicationPK"
class="org.example.composite.ApplicationPK" >

<key-property name="collectorateCode" column="COLLECTORATE_CODE" type="string" />
<key-property name="applicationNumber" column="APPL_SNO" type="integer"/>
<key-property name="year" column="YEAR_NBR" type="integer"/>

</composite-id>

<!-- Normal properties of a java class -->
<property name="name" column="NAME" type="stringr"></property>
<property name="age" column="AGE" type="integer"></property>

</class>

</hibernate-mapping>


Here is an example of the Composite key class
public class Application {
private ApplicationPK applicationPK;
//declare other properties
//declare constructor
}
public class ApplicationPK implements Serializable {

//declare composite properties
// declare constructor

}

How to call the query
Note you need to set the required setter method in the composite key class
ht.load(Application.class,applicationPK)



BUT I dont understand what does ht.load(Application.class,applicationPK) mean ?

Please clarify if possible.
I went thru hibernate docs but don't understand how to use it.


Thanks......................
 
srija patel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul, I got the solution after overriding the equals and hashcode() methods.......

Yay!!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic