Hibernate一对一双向映射XML

时间:2016-11-16 18:40:50

标签: java xml hibernate

我有3张表,如下图所示:

enter image description here

我正在尝试构建一个REST API,当通过GET方法访问/ template时, 返回的JSON将包含templateDefinition对象,该对象还包含模板类型对象。为此,我创建了以下映射文件:

Template.hbm.xml

<hibernate-mapping package="com.reporttemplateengine.models">
    <class name="Template" table="TEMPLATE">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="name" column="NAME" type="string" />
        <one-to-one name="templateDefinition" class="com.reporttemplateengine.models.TemplateDefinition" constrained="true"></one-to-one>
    </class> 
</hibernate-mapping>

TemplateType.hbm.xml

<hibernate-mapping package="com.reporttemplateengine.models">
    <class name="TemplateType" table="TEMPLATETYPE">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="name" column="NAME" type="string" />
    </class> 
</hibernate-mapping>

TemplateDefinition.hbm.xml

<hibernate-mapping package="com.reporttemplateengine.models">
    <class name="TemplateDefinition" table="TEMPLATEDEFINITION">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="identity" />
        </id>
        <property name="templateFile" column="TEMPLATE_FILE" type="blob" />
        <property name="version" column="VERSION" type="int" />
        <property name="active" column="ACTIVE" type="int" />
        <many-to-one name="template" class="com.reporttemplateengine.models.Template"
            column="template_id" unique="true" not-null="true" cascade="all" />
        <many-to-one name="templateType" class="com.reporttemplateengine.models.TemplateType"
        column="template_type_id" unique="true" not-null="true" cascade="all" />
    </class> 
</hibernate-mapping>

当我调用API时,Hibernate会记录此查询:

select
        this_.ID as ID1_0_1_,
        this_.NAME as NAME2_0_1_,
        templatede2_.ID as ID1_1_0_,
        templatede2_.TEMPLATE_FILE as TEMPLATE_FILE2_1_0_,
        templatede2_.VERSION as VERSION3_1_0_,
        templatede2_.ACTIVE as ACTIVE4_1_0_,
        templatede2_.TEMPLATE_ID as TEMPLATE_ID5_1_0_,
        templatede2_.TEMPLATE_TYPE_ID as TEMPLATE_TYPE_ID6_1_0_ 
    from
        SYSTEM.TEMPLATE this_ 
    left outer join
        SYSTEM.TEMPLATEDEFINITION templatede2_ 
            on this_.ID=templatede2_.ID

它应该看起来像这样:

select
        this_.ID as ID1_0_1_,
        this_.NAME as NAME2_0_1_,
        templatede2_.ID as ID1_1_0_,
        templatede2_.TEMPLATE_FILE as TEMPLATE_FILE2_1_0_,
        templatede2_.VERSION as VERSION3_1_0_,
        templatede2_.ACTIVE as ACTIVE4_1_0_,
        templatede2_.TEMPLATE_ID as TEMPLATE_ID5_1_0_,
        templatede2_.TEMPLATE_TYPE_ID as TEMPLATE_TYPE_ID6_1_0_ 
    from
        SYSTEM.TEMPLATE this_ 
    inner join
        SYSTEM.TEMPLATEDEFINITION templatede2_ 
            on this_.ID=templatede2_.TEMPLATE_ID

这也缺少嵌套在templateDefinition中的模板类型对象。我不知道如何实现这一点。

0 个答案:

没有答案