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]