-->
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.  [ 1 post ] 
Author Message
 Post subject: update and delete: parent and child relationship
PostPosted: Mon May 14, 2007 3:32 am 
Beginner
Beginner

Joined: Sat Mar 03, 2007 5:32 am
Posts: 20
i have a parent table: tbl_document, child table: tbl_attachment.
Problem 1:
currently when i do delete, it only delete the data from parent table. My code as follow:

Action class:
Code:
  String id = request.getParameter("Id");
        List rshPpr = docSrv.getRshPaperTitle(Long.parseLong(id));
        RshPaper rp =(RshPaper) rshPpr.get(0);
        String title= rp.getTitle();
        request.setAttribute("title", title);
        docSrv.remove(Long.parseLong(id));


Service class:
Code:

public boolean removeResearchPaper(long id) throws Exception
    {
        final String _METHOD = "[removeResearchPaper] ";
        boolean bResult = false;
        try
        {
            RshPaper oRshPaper = new RshPaper();
            oRshPaper.setId(id);
            oSession = HibernateUtils.openSession();
            HibernateAction.delete(oSession, oRshPaper);
            log.debug(_METHOD + "bResult=" + bResult);
            HibernateUtils.flushSession();
            return bResult;
        }
        catch (HibernateException e)
        {
            log.error(_METHOD + e);
            throw e;
        }
        finally
        {
            if (oSession!=null)
                HibernateUtils.closeSession();
        }
    }


What do i need to add on so that it will also delete the child datas.

Problem 2:
When i do the update without attaching any attachment, the child will be update with null. but i want the child to actually dont do update when no attachment is attached.

Action class:
Code:
      RshPaper oRshPaper = new RshPaper();
        RshPaperForm oForm = (RshPaperForm) form;
        Date oDate = new Date();
   
        oRshPaper .setId(oForm.getId());
        oRshPaper .setTitle(oForm.getTitle());
        oRshPaper .setSummary(oForm.getSummary());
        oRshPaper .setSubmitUserId(oForm.getSubmitUserId());       
        oRshPaper .setModifiedDate(oDate);
        oRshPaper .setDocStatus(1); 
               
        if(oForm.getFile() != null){
            if(oForm.getFile().getFileSize()>0){
                oRshPaper .setFiles(new HashSet());
                oAtt.setFile(oForm.getFile().getFileData());
                oAtt.setFilename(oForm.getFile().getFileName());
                oResearchPaper.getFiles().add(oAtt);
                oAtt.setRshPaper(oRshPaper );
            }
        } 

            docSrv.updateRshPaperDetails(oRshPaper);


Service Class:
Code:
public void updateRshPaperDetails(RshPaper oRshPaper){
        try {
            oSession = HibernateUtils.openSession();       
            HibernateAction.saveOrUpdate(oSession, oRshPaper);
            HibernateUtils.flushSession();       
        } catch (HibernateException e)  {
            throw e;
        } finally {
            if (oSession!=null)
                HibernateUtils.closeSession();
        }
    }


Mapping
Code:
Parent Class

<hibernate-mapping>
    <class name="com.RshPaper" table="DOCUMENT">
        <id name="id" column="ID">
            <generator class="increment"/>
        </id>
        <set name="files" cascade="save-update" lazy="false">
           <key column="DOCUMENT_ID" not-null="false"/>
           <one-to-many class="com.Attachment"/>
        </set>       
        <property name="creationDate" type="java.util.Date" column="CREATED_DATE"/>       
        <property name="title" type="java.lang.String" column="TITLE"/>
        <property name="summary" type="java.lang.String" column="LONG_DESC"/>       
        <property name="modifiedDate" type="java.util.Date" column="LAST_MODIFY_DATE"/>
        <property name="docType" type="java.lang.Integer" column="DOCUMENT_TYPE_ID"/>
        <property name="submitUserId" type="java.lang.Long" column="SUBMIT_USER_ID"/>
        <property name="docStatus" type="java.lang.Integer" column="DOCUMENT_STATUS"/>
    </class>       
</hibernate-mapping>


Child Class
<hibernate-mapping>
    <class name="com.Attachment" table="USER_DOCUMENT_ATTACHMENT">
        <id name="id" column="ID">
            <generator class="increment"/>
        </id>        
        <many-to-one name="researchPaper"
                     class="com.RshPaper"
                     not-null="true" column ="DOCUMENT_ID"/> 
        <property name="file" type="binary" column="BFILE"/>
        <property name="filename" type="java.lang.String" column="FILENAME"/>       
    </class>
</hibernate-mapping>
[/code]


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

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.