子类和单个字段的hibernate映射

时间:2011-01-13 13:43:21

标签: java hibernate

我有这个类层次结构:

class A
{
  private String a1;
  private Class B a2;
// getter & setter
}

class B
{
  private String b1;
  private String b2;
  // getter & setter
}
class C extends class A
{
  private String c1;
  // getter and setter for c1;
}

我需要使用hibernate将C映射到数据库中的单个表。我试试这个:

<class name="com.C" 
    table="myTable" catalog="myCatalog"
    polymorphism="implicit">
    <property name="a1" type="string">
            <column name="column1" length="40" not-null="true" />
    </property>
    <property name="a2" type="????">
            ?? B should map to column2 and column3
    </property>
    <property name="c1" type="string">
            <column name="column4" length="40" not-null="true" />
    </property>
</class>

如何映射B类的字段b?

谢谢,

1 个答案:

答案 0 :(得分:1)

您好发现它使用组件标签:

<class name="com.C" 
    table="myTable" catalog="myCatalog"
    polymorphism="implicit">
    <property name="a1" type="string">
            <column name="column1" length="40" not-null="true" />
    </property>
    <component name="a2" class="com.B">
        <property name="b1" type="java.lang.Integer">
            <column name="column2" />
        </property>
        <property name="b2" type="java.lang.Integer">
            <column name="column3" />
        </property>        
    </component> 
    <property name="c1" type="string">
            <column name="column4" length="40" not-null="true" />
    </property>
</class>