在hibernate中嵌套一对多关系的CRUD操作

时间:2015-05-21 03:39:30

标签: hibernate

我的表之间有以下关系:

             one-to-one                   one-to-many

ESReportTemplate< ------------> SearchPropsObject< ----> FacetProps

我的所有表都已成功创建,如果添加ESReportTemplate对象,则会正确添加所有子对象。但是,如果我检索SearchPropsObject,修改它的属性并在ESReportTemplate上执行更新对象,则更改不会反映在数据库中。

我的hbm文件如下:

ESReportTemplate.hbm.xml:

<hibernate-mapping>
<class dynamic-insert="true" dynamic-update="true" select-before-update="true" discriminator-value="ESReportTemplate" name="com.webnms.nms.esreport.ESReportTemplate" table="ESReportTemplate">
<cache usage="read-write"/>
        <id column="ESTID" name="estId" type="java.lang.Long">
            <generator class="increment"/>
        </id>
    <natural-id>
    <property column="NAME" length="100" name="name" not-null="true" type="string"/>
    </natural-id>
    <set name="esReports" inverse="true" lazy="false" cascade="all">
                        <cache usage="read-write"/>
                    <key column="ESTID"/>
                    <one-to-many class="com.webnms.nms.esreport.ESReportObject" />
</set>
<one-to-one name="SearchPropsObject" class="com.webnms.nms.esreport.SearchPropsObject" cascade="all" lazy="false"></one-to-one>
</class>
</hibernate-mapping> 

SearchPropsObject.hbm.xml:

<hibernate-mapping>
<class dynamic-insert="true" dynamic-update="true" select-before-update="true" discriminator-value="SearchPropsObject" name="com.webnms.nms.esreport.SearchPropsObject" table="SearchPropsObject">
<cache usage="read-write"/>
<id name="estId" type="long" column="ESTID">
               <generator class="foreign">
                <param name="property">ESReportTemplate</param>
            </generator>
</id>
<one-to-one name="ESReportTemplate" class="com.webnms.nms.esreport.ESReportTemplate" constrained="true" lazy="false"></one-to-one>  
<property column="REPORTTYPE" length="100" name="reportType" type="string"/>
<property column="PERSONALITYNAMES" length="500" name="personalityNames" type="string"/>
<property column="APPNAMES" length="500" name="appNames" type="string"/>
<property column="ENTITYNAMES" length="500" name="entityNames" type="string"/>
<set name="facets" inverse="true" lazy="false" cascade="all">
                        <cache usage="read-write"/>
                    <key column="ESTID"/>
                    <one-to-many class="com.webnms.nms.esreport.FacetProps" />
</set>
</class>
</hibernate-mapping>

FacetProps.hbm.xml

<hibernate-mapping>
<class dynamic-insert="true" dynamic-update="true" select-before-update="true" discriminator-value="FacetProps" name="com.webnms.nms.esreport.FacetProps" table="FacetProps">
<cache usage="read-write"/>
        <id column="FID" name="facetId" type="java.lang.Long">
            <generator class="increment"/>
        </id>
<many-to-one name="searchProps" class="com.webnms.nms.esreport.SearchPropsObject" fetch="select">
<column name="ESTID" not-null="true"/>
</many-to-one>
<property column="FACETS" length="500" name="facets" type="string"/>
<property column="ISMULTILEVEL" length="100" name="isMultiLevel" type="boolean"/>
</class>
</hibernate-mapping>

我的更新代码如下:

ESReportTemplate est = (ESReportTemplate)obj;
session.saveOrUpdate(est);

注意:会话由我们的应用程序在内部创建和管理。

感谢任何帮助。我不确定我是不是想念。

0 个答案:

没有答案