tags:

views:

30

answers:

1

I have a few simple entities that are stored in a simple "code" table with the following structure (all stored as character fields):

code_fieldname   pk
code_value       pk
code_comment
code_field1
code_field2

One object I am trying to map has data stored in this table of the following form

code_fieldname = "segment"
class = Segment
segment.id = code_value
segment.name = code_comment
segment.markup = code_field1 (decimal property)

So the segment records all have a "code_fieldname" = "segment" with their ID being the value of "code_value".

What is the mapping for such a structure? I am using XML based mapping.

A: 

I've managed to solve this first by using inheritance for the class, and then using a formula property on the relationship. Definition for the entities follow:

<class name="GeneralCode" table="code_mstr">
  <composite-id name="key">
    <key-property name="key" column="code_fieldName" type="string"/>
    <key-property name="value" column="code_value" type="string"/>
  </composite-id>
  <discriminator column="code_fieldName" type="string" insert="false"/>
  <subclass name="Segment" discriminator-value="segment">
    <property name="name" type="string" column="code_comment"/>
  </subclass>
</class>

Now I can use this in a relationship like the following:

<many-to-one name="segment" class="Segment"
             lazy="false" insert="false" update="false">
  <formula>'segment'</formula>
  <column name="segment"/>
</many-to-one>

Note that the formula requires the quoted 'segment' value as this is always the fields key, i.e. code_fieldname = 'segment' in all cases.

Brett Ryan