2014-03-19 17 views
0

reports.hbm.xml在許多到一個休眠關係刪除記錄不工作

<hibernate-mapping> 
     <class name="com.srdiagnostic.app.bdo.Reports" table="REPORTS" schema="SR"> 
    <id name="reportsId" type="long"> 
     <column name="REPORTS_ID" precision="22" scale="0" /> 
     <generator class="increment"/> 
    </id> 
    <property name="reportDate" type="date"> 
     <column name="REPORT_DATE" length="7" /> 
    </property> 
    <many-to-one name="patient" class="com.srdiagnostic.app.bdo.Patient" fetch="select" cascade="all" unique="true" lazy="false"> 
     <column name="PATIENT_ID" precision="22" scale="0" /> 
    </many-to-one> 
    <many-to-one name="doctor" class="com.srdiagnostic.app.bdo.Doctor" fetch="select" cascade="all" unique="true" lazy="false"> 
     <column name="doctor_ID" precision="22" scale="0" /> 
    </many-to-one> 

我能夠在一個時間報告和醫生表正確插入記錄,但無法刪除一次報告和醫生的記錄。同時刪除報告表中的記錄時,它僅在報告表中刪除,但一次不能刪除在醫生表中。

Dao class 
    public void deleteReport(Reports report) throws AppException, AppSysException { 
    Session session = SRHibernateUtil.getSessionFactory().openSession(); 
    session.beginTransaction(); 
    session.delete(report); 
    session.getTransaction().commit(); 
    System.out.println("Report deleted successfully"); 
    } 
    Reports.java 
    public class Reports implements java.io.Serializable { 
    private Long reportsId; 
    private Date reportDate; 
    private Patient patient; 
    private Doctor doctor; 
    //setters&getters } 
    Doctor.java 
    public class Doctor implements java.io.Serializable { 
    private long doctorId; 
    private String doctorName; 
    private String place;  
    //setters&getters } 
+0

分享您的實體bean和DAO類..錯誤信息,如果有的話 – BDR

回答

2

「它(是)只在報告表中刪除,但一次不能刪除在醫生表中。」

關係僅在多方面存在,即。該報告擁有外鍵參考相關醫生。刪除報告時,您無需在醫生表中刪除任何內容。

事實是,醫生可能會被許多報告引用,因此您不能在刪除單個報告時觸發醫生刪除。

如果你的目標是刪除從醫生的報告(由醫生的報告集合中移除)刪除孤兒將做的工作(orphanRemoval =上關係的一對多方真)

+0

你正確的醫生被許多報告引用,我做錯了。 – techLearner

+0

但我想刪除一個報告稱爲CbpReport它是forigenkey在報告中,當我刪除報告中的記錄無法刪除CbpReport.shall我把一對多的關係放在Cbpreport.hbm.xml中? – techLearner

+0

1報告可能有很多CbpReport?你想刪除報告觸發相關的CbpReport刪除? =>帶有級聯刪除的報告中的OneToMany CbpReport – Gab