Jothi
Jothi

Reputation: 15090

How to avoid updating the child object while updating parent in hibernate?

I don't knwo whether my desig itself wrong or what. but could any body give me a solution. I have a Object, PlanningItem. From this i need to get the records form another table by using two properties of PlanningItem. so i created view and and added that view as one to many relation record in PlanningItem. when i was updating the Planning Item i am getting error. In this i am ready to change view into and subselect or anything else. please suggest the idea to proceed.

Below is my code.

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>

database View

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;

Upvotes: 1

Views: 2646

Answers (2)

Vala
Vala

Reputation: 5674

This isn't how you would generally use Hibernate. Associate your objects directly with the tables in question, you can then define your queries to fetch them with the filtering etc. that you're putting in your view. I recommend reading up on JPQL for querying with JPA objects.

Upvotes: 0

dimas
dimas

Reputation: 6073

Try to set in your one-to-many side of relationship parameter inverse="true". This will tell hiberante not to update relationship (FK column) when you are updating parent.

I mean next:

<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>

Upvotes: 1

Related Questions