views:

43

answers:

3

I am working on converting a legacy system to use hibernate (version 3.3.x) instead of using hand crafted SQL. I have run in to some problems mapping my datamodel that pertians to composite keys. I've created a solution I think works, but I am not overly fond of it. Hence, I would like to see how the diagram below could/should be mapped and see if I am on the "right" track.

alt text

In the diagram StuffTypes is a pretty static table that don't change (no inserts or updates). Parent is the only table that currently has a DAO class associated to it (the others should be persisted when the parent instance is). Stuff has a StuffType and a number of SubStuff associated with it. Finally, SubStuff is just a many to many mapping table between Stuff and StuffTypes.

What is the best way of mapping these entities to Java objects using annotations?

A: 

Adding a primary key stuff_id to the Stuff table and another primary key substuff_id to SubStuff is less complicated. Composite keys are possible, of course. If seen solutions where @Embeddable classes have been introduced to model the composite keys.

Andreas_D
A: 

Hibernate Reference is your best bet. Try this, Mapping Entities with Composite Keys.

Adeel Ansari
+1  A: 

Personally, I often refer to the section 3.2 Primary Keys through -ToOne Relationships of the JPA Wiki Book. And read also 3.1 Composite Primary Keys.

Pascal Thivent