Map denormalized hibernate
-
30-09-2019 - |
문제
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)?
해결책
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).
다른 팁
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.