views:

131

answers:

3

I have a Summary class which contains a list of Qualities. A Quality contains a String name and int value. This data is stored in a denormalized db structure, one table only, for both Summary and Quality.

Quality table:

id, somefileds, qualityname1, qualityvalue1, qualityname2, qualityvalue2, qualityname3, qualityvalue3

For each quality name & value pairs, a new Quality object must be inserted in the Summary class.

How to map this in hibernate (xml hibernate mapping)?

+2  A: 

It is not clear how Quality objects must be "inserted" in the Summary holder class but I think that a custom user type (either o.h.u.UserType or o.h.u.UserCollectionType) is the way to go here.

Refer to the section 5.2.3. Custom value types in the documentation for more details (there is not much to say about the mapping, simply specify your custom user type as type in the mapping).

Pascal Thivent
A: 

I managed to fix it with a custom CompositeUserType implementation. The mapping file looks as follows:

<property name="qualities" type="com.foo.bar.QualityCompositeUserType">
        <column name="linkName1" />
        <column name="linkQuality1" />
        <column name="linkName2" />
        <column name="linkQuality2" />
        <column name="linkName3" />
        <column name="linkQuality3" />
</property>

The returnedClass in the CompositeUserType is List.class, the nullSafeSet method gets the collection as value argument. It is just a matter of getting the values from the list and assign them to the parameters in the prepared statement. (and fill with null if values are missing in the list of Qualities).

The nullSafeGet method is easyer. I just create a new ArrayList in which new Quality objects are inserted with values from the resultset.

I can add a full example if interested.

Jurgen H
A: 

Thought I'd be able to comment on your answer, Jurgen, but I'll have to post another "answer" instead. I'd be interested in the full example if you still have the source around. Thanks

emtrane