如何在hibernate中更新父进程时避免更新子对象?

时间:2012-08-03 08:22:47

标签: java hibernate

我不知道我的设计本身是错还是什么。但任何机构都可以给我一个解决方案。 我有一个Object,PlanningItem。从这里我需要通过使用PlanningItem的两个属性从另一个表中获取记录。所以我创建了视图,并将该视图添加为PlanningItem中的一对多关系记录。当我更新规划项目时,我收到错误。 在此我准备将视图更改为subselect或其他任何内容。请建议继续这个想法。

以下是我的代码。

ViewPlannigItem.hbm.xml

<class
        name="com.cotyww.ipmasterdata.core.entity.master.ViewPlannigItem" 
        table="VW_PLANNING_ITEM"  >
        <id name="planningIrc" type="string" column="PLANNING_IRC">
            <generator class="assigned" />
        </id>

        <property name="planningIRCDesc" column="PLANNING_ITEM_DESC"
            type="string" />


        <property name="programId" column="PROGRAM_ID" type="string" />


        <property name="salesOrgId" column="SALES_ORG_ID"
            type="integer" />

        <property name="salesBrandId" column="SALES_BRAND_PKEY"
            type="integer" />

        <many-to-one name="salesBrand" update="false" insert="false"
            class="com.cotyww.ipmasterdata.core.entity.master.SalesBrand"
            column="SALES_BRAND_PKEY" />

        <many-to-one name="salesOrg" update="false" insert="false"
            class="com.cotyww.ipmasterdata.core.entity.master.SalesOrg"
            column="SALES_ORG_ID" />

    </class>

PlanningItem.hbm.xml

<class 
        name="com.cotyww.ipmasterdata.core.entity.master.PlanningItem"
        table="Planning_Item">
        <id name="planningIrc" type="string" column="PLANNING_IRC">
            <generator class="assigned" />
        </id>

        <property name="programId" column="Program_Id" type="string" />

        <property name="planningitemDesc" column="Planning_Item_Desc"
            type="string" />

        <many-to-one name="hierarchyCode" update="false" insert="false"
            class="com.cotyww.ipmasterdata.core.entity.master.GmmBrand"
            column="Gmm_Hierarchy_Code" />

        <property name="gmmhierarchyCode" column="Gmm_Hierarchy_Code"
            type="string" />

        <many-to-one name="gmmcategoryCode" update="false"
            insert="false"
            class="com.cotyww.ipmasterdata.core.entity.master.GmmCategory"
            column="Category_Code" />
        <property name="categoryCode" column="Category_Code"
            type="string" />

        <set name="lstViewPlanningItem"  cascade="none" >
            <key column="PLANNING_IRC" />
            <one-to-many not-found="ignore" 
                class="com.cotyww.ipmasterdata.core.entity.master.ViewPlannigItem" />
            <filter name="planningsalesORg"
                condition="SALES_ORG_ID = :salesOrgId" />
        </set>

        <property name="modifiedBy" column="Audit_User" type="string" />
        <property name="modifiedDate" column="Audit_Date" type="date" />
        <property name="createdBy" column="Created_By" type="string" />
        <property name="createdDate" column="Created_Date" type="date" />

    </class>
    <sql-query name="nextPlanningItemSequenceValueC1US">
        <![CDATA[ SELECT SQ_PLANNING_ITEM_PKEY_C1US.nextVal FROM dual ]]>
    </sql-query>

        <sql-query name="nextPlanningItemSequenceValueC1CA">
        <![CDATA[ SELECT SQ_PLANNING_ITEM_PKEY_C1CA.nextVal FROM dual ]]>
    </sql-query>

        <sql-query name="nextPlanningItemSequenceValueL1US">
        <![CDATA[ SELECT SQ_PLANNING_ITEM_PKEY_L1US.nextVal FROM dual ]]>
    </sql-query>

        <sql-query name="nextPlanningItemSequenceValueL1CA">
        <![CDATA[ SELECT SQ_PLANNING_ITEM_PKEY_L1CA.nextVal FROM dual ]]>
    </sql-query>

        <sql-query name="nextPlanningItemSequenceValueP1US">
        <![CDATA[ SELECT SQ_PLANNING_ITEM_PKEY_P1US.nextVal FROM dual ]]>
    </sql-query>

        <sql-query name="nextPlanningItemSequenceValueP1CA">
        <![CDATA[ SELECT SQ_PLANNING_ITEM_PKEY_P1CA.nextVal FROM dual ]]>
    </sql-query>
    <filter-def name="planningsalesORg">
        <filter-param name="salesOrgId" type="integer" />
    </filter-def>

数据库视图

CREATE OR REPLACE FORCE VIEW COREMASTER.VW_PLANNING_ITEM
(PLANNING_IRC, PLANNING_ITEM_DESC, PROGRAM_ID, SALES_ORG_ID, SALES_BRAND_PKEY, 
 GMM_HIERARCHY_CODE)
AS 
SELECT  P.PLANNING_IRC,
            P.PLANNING_ITEM_DESC,
            P.PROGRAM_ID,
            nvl(MAP.SALES_ORG_ID,MAP1.SALES_ORG_ID) as sales_org,
            nvl(MAP.SALES_BRAND_PKEY,MAP1.SALES_BRAND_PKEY) as sales_brand_pkey,
            P.GMM_HIERARCHY_CODE
     FROM   PLANNING_ITEM P,
            GMM_MAP MAP,
            GMM_MAP MAP1    
     WHERE  
            P.GMM_HIERARCHY_CODE = MAP.GMM_HIERARCHY_CODE(+)
            AND P.CATEGORY_CODE = Map.GMM_CATEGORY_CODE(+)
            AND P.GMM_HIERARCHY_CODE = MAP1.GMM_HIERARCHY_CODE(+)
            AND Map1.GMM_CATEGORY_CODE IS NULL;

2 个答案:

答案 0 :(得分:1)

尝试设置关系参数inverse =“true”的一对多方面。这将告诉hiberante在更新父级时不要更新关系(FK列)。

我的意思是下一个:

<set name="lstViewPlanningItem"  cascade="none"  inverse="true">
         <key column="PLANNING_IRC" />
         <one-to-many not-found="ignore"
                class="com.cotyww.ipmasterdata.core.entity.master.ViewPlannigItem" />
         <filter name="planningsalesORg"
                condition="SALES_ORG_ID = :salesOrgId" />
</set>

答案 1 :(得分:0)

这不是你通常使用Hibernate的方式。将您的对象直接与相关表格相关联,然后您可以定义查询以使用您在视图中放置的过滤等来获取它们。我建议阅读JPQL以查询JPA对象。