-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Adding a Child In Parent/Child Relationship
PostPosted: Wed Sep 28, 2005 4:35 pm 
Beginner
Beginner

Joined: Tue Sep 06, 2005 5:16 pm
Posts: 24
Location: USA
Hibernate version:2.1.8


Name and version of the database you are using:oracle9i



Hi There i have a generel question regarding Adding a Child In Parent/Child Relationship..

I have a parent with Parent which contains 2 children ..I want to add 3rd child...Iam following this procedure..but iam unable to add the 3rd child..Iam not understanding where exactly iam doing the mistake..

1.Loaded the Parent with the key
2.got the ChildSet from the Parent
3.created the object with 3rd child's details
4.added the created object(Step 3) to the Set i got from step 2
5.added the modified set(from step 4) to the Parent that from step 1
6.used to following code

Code:
      session.saveOrUpdate(cswCaseParent);
      session.flush();
      tx.commit();


Still iam unable to see the 3rd child in the data base..Am i not following correct procedure??Please help me...

Thanks In Advance

JD


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 4:57 pm 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
post the mapping files.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 5:11 pm 
Beginner
Beginner

Joined: Tue Sep 06, 2005 5:16 pm
Posts: 24
Location: USA
Mapping file for parent:

Code:
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
                            "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
                            "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration.                   -->
<!-- Created Wed Sep 07 16:56:45 EDT 2005                         -->
<hibernate-mapping package="**.datatransfer" auto-import="true">

    <class name="CswCaseParent1" table="CSWDBA.CSW_CASE_PARENT_1">
        <composite-id name="id" class="CswCaseParent1Key">
            <key-property name="caseId" column="CASE_ID" type="java.lang.String"/>
            <key-property name="countyCd" column="COUNTY_CD" type="java.lang.String"/>
        </composite-id>

        <property name="clientSsnId" column="CLIENT_SSN_ID" type="java.lang.String" />
        <property name="pinId" column="PIN_ID" type="java.lang.String"  not-null="true" />
        <property name="clientPinId" column="CLIENT_PIN_ID" type="java.lang.String"  not-null="true" />
        <property name="clientNm" column="CLIENT_NM" type="java.lang.String" />
        <property name="netDueAmt" column="NET_DUE_AMT" type="java.lang.Double"  not-null="true" />
        <property name="respondentBirthDt" column="RESPONDENT_BIRTH_DT" type="java.util.Date" />
        <property name="respondentAddressTypeCd" column="RESPONDENT_ADDRESS_TYPE_CD" type="java.lang.String" />
        <property name="respondentUpdateSsnId" column="RESPONDENT_UPDATE_SSN_ID" type="java.lang.String" />
        <property name="paymentFromDt" column="PAYMENT_FROM_DT" type="java.util.Date"  not-null="true" />
        <property name="paymentToDt" column="PAYMENT_TO_DT" type="java.util.Date"  not-null="true" />
        <property name="respondentSsnId" column="RESPONDENT_SSN_ID" type="java.lang.String" />
        <property name="respondentPinId" column="RESPONDENT_PIN_ID" type="java.lang.String"  not-null="true" />
        <property name="respondentNm" column="RESPONDENT_NM" type="java.lang.String" />
        <property name="createDt" column="CREATE_DT" type="java.util.Date"  not-null="true" />
        <property name="modifyDt" column="MODIFY_DT" type="java.util.Date" />
        <property name="controlCycleNbr" column="CONTROL_CYCLE_NBR" type="java.lang.Integer"  not-null="true" />
        <property name="ncpUpdateDt" column="NCP_UPDATE_DT" type="java.util.Date" />


     <!-- 
        <set name="cswChild1Set" inverse="true">
            <key>
                <column name="CASE_ID"/>
               <column name="COUNTY_CD"/>
            </key>
            <one-to-many class="**.CswChild1"/>
        </set>



        <set name="cswChild11Set" inverse="true">
            <key column="COUNTY_CD"/>
            <one-to-many class="**.CswChild1"/>
        </set>
   -->
        <set name="cswDisbursement1Set" inverse="true">
            <key>
                <column name="CASE_ID"/>
               <column name="COUNTY_CD"/>
            </key>
            <one-to-many class="**.CswDisbursement1"/>
        </set>
<!--
        <set name="cswDisbursement11Set" inverse="true">
            <key column="COUNTY_CD"/>
            <one-to-many class="**.CswDisbursement1"/>
        </set>
-->
        <set name="cswEmployer1Set" inverse="true">
            <key>
                <column name="CASE_ID"/>
               <column name="COUNTY_CD"/>
            </key>
            <one-to-many class="**.CswEmployer1"/>
        </set>
<!--
        <set name="cswEmployer11Set" inverse="true">
            <key column="COUNTY_CD"/>
            <one-to-many class="**.CswEmployer1"/>
        </set>

        <set name="cswMedicalInsurance1Set" inverse="true">
            <key column="CASE_ID"/>
            <one-to-many class="**.CswMedicalInsurance1"/>
        </set>

        <set name="cswMedicalInsurance11Set" inverse="true">
            <key column="COUNTY_CD"/>
            <one-to-many class="**.CswMedicalInsurance1"/>
        </set>
-->
        <set name="cswPayment1Set" inverse="true" >
            <key>
                <column name="CASE_ID"/>
               <column name="COUNTY_CD"/>
            </key>
            <one-to-many class="**.CswPayment1"/>
        </set>
<!-- 
        <set name="cswPayment11Set" inverse="true">
            <key column="COUNTY_CD"/>
            <one-to-many class="**.CswPayment1"/>
        </set> -->
    </class>
   
</hibernate-mapping>


Mapping file for Child:

Code:
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
                            "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
                            "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration.                   -->
<!-- Created Wed Sep 07 17:16:34 EDT 2005                         -->
<hibernate-mapping package="**.datatransfer" auto-import="true">

    <class name="CswEmployer1" table="CSWDBA.CSW_EMPLOYER_1">
        <id name="employerId" column="EMPLOYER_ID" type="java.lang.Long">
            <generator class="increment"/>
        </id>

        <property name="employerNm" column="EMPLOYER_NM" type="java.lang.String"  not-null="true" />
        <property name="createDt" column="CREATE_DT" type="java.util.Date"  not-null="true" />
        <property name="modifyDt" column="MODIFY_DT" type="java.util.Date" />

        <many-to-one

           name="cswCaseParent1"
           class="**.CswCaseParent1" 
           not-null="true"
           update="true"
           insert="false">
         <column name="CASE_ID"/>           
         <column name="COUNTY_CD"/>

    </many-to-one>


<!--         <many-to-one name="cswCaseParent1" column="CASE_ID" class="CswCaseParent1"  not-null="true" />

        <many-to-one name="cswCaseParent11" column="COUNTY_CD" class="CswCaseParent1"  not-null="true" /> -->
    </class>
   
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 5:30 pm 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
try with insert="true" in the child.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 5:34 pm 
Beginner
Beginner

Joined: Tue Sep 06, 2005 5:16 pm
Posts: 24
Location: USA
its not working even with

Code:
insert="true"


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 1:42 am 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
jdsrinivas wrote:
its not working even with

Code:
insert="true"


can you post the java code


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 8:44 am 
Beginner
Beginner

Joined: Tue Sep 06, 2005 5:16 pm
Posts: 24
Location: USA
Sorry i was not in the office..Here is the Java Code

Code:
   public void insertEmployer1(){

      try{
      CswCaseParent1 cswCaseParent = new CswCaseParent1();
      Set cswe = new HashSet();
      Session session = HibernateSessionFactory.currentSession();
      Transaction tx = session.beginTransaction();
      CswCaseParent1Key ccp = new CswCaseParent1Key();
      ccp.setCaseId("EF12345A1");
      ccp.setCountyCd("01");
      cswCaseParent = (CswCaseParent1)session.load(CswCaseParent1.class, ccp);
      
      System.out.println("Got the employer Set..");
      CswEmployer1 cswe1 = new CswEmployer1();
      //cswe1.setCswCaseParent1(ccp);
      cswe1.setEmployerNm("SRI");
      cswe1.setEmployerId(new Long(10));
      cswe1.setCreateDt(new Date());
      cswe1.setModifyDt(new Date());
      System.out.println("Constructed new employer Object ..");
      cswe1.setCswCaseParent1(cswCaseParent);
      cswe = (Set)cswCaseParent.getCswEmployer1Set();
   
      System.out.println("Before Adding the new object to  employer Set.."+cswe.size());
      cswe.add(cswe1);
      System.out.println("Added the new object to  employer Set.."+cswe.size());
      cswCaseParent.setCswEmployer1Set(cswe);
      System.out.println("Added Modified employer Set to the Parent..");
      session.saveOrUpdate(cswCaseParent);
      System.out.println("Saved the modified Parent");
      session.flush();
      System.out.println("Flushed..");
      tx.commit();
      System.out.println("Committed..");
      }catch (HibernateException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }finally{
         try {
            HibernateSessionFactory.closeSession();
         } catch (HibernateException e1) {
            e1.printStackTrace();
         }
      }
      

      
   }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 9:00 am 
Beginner
Beginner

Joined: Tue Sep 06, 2005 5:16 pm
Posts: 24
Location: USA
This is more clean java code

Code:
   public void insertEmployer1(){
      try{
      Session session = HibernateSessionFactory.currentSession();
      Transaction tx = session.beginTransaction();
      
      CswCaseParent1 cswCaseParent = new CswCaseParent1();      
      CswCaseParent1Key ccp = new CswCaseParent1Key();
      ccp.setCaseId("EF12345A1");
      ccp.setCountyCd("01");
      cswCaseParent = (CswCaseParent1)session.load(CswCaseParent1.class, ccp);
      
      CswEmployer1 cswe1 = new CswEmployer1();
      cswe1.setEmployerNm("SRI");
      cswe1.setEmployerId(new Long(10));
      cswe1.setCreateDt(new Date());
      cswe1.setModifyDt(new Date());

      Set cswe = new HashSet();
      cswe = (Set)cswCaseParent.getCswEmployer1Set();
      cswe.add(cswe1);

      cswCaseParent.setCswEmployer1Set(cswe);

      session.saveOrUpdate(cswCaseParent);
      session.flush();
      tx.commit();
      }catch (HibernateException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }finally{
         try {
            HibernateSessionFactory.closeSession();
         } catch (HibernateException e1) {
            e1.printStackTrace();
         }
      }
   }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 12:08 am 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
do a
Code:
cswe1.setCswCaseParent1(cswCaseParent );


you need to do a child.setParent() and it should work.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.