2012-03-26 17 views
1

Ich bin neu im Winterschlaf. Dies ist mein Master visa Bean Bean Mapping, die viele Eins-zu-viele und viele-zu-viele Beziehung Mapping durch Winterschlaf hat. Jetzt den Datensatz löscht Ich bin mit dieser AbfrageHibernate löschen verwandte Tabellen mit löschen HQL?

Transaction ts=session.beginTransaction(); 
       String hql="delete from VisaInquiry inq where inquiryId="+inqId; 
       Query query=session.createQuery(hql); 
       query.executeUpdate(); 
       ts.commit(); 

Aber Problem ist, dass es nur Datensätze aus dem Haupt table.And löscht alle Daten auf anderen Tabellen bleiben, wie es ist. Ich möchte alle Datensätze aus allen verwandten Fremdtabelle löschen .. Bitte Hilfe?

<id name="inquiryId" column="inquiryId"> 
     <generator class="increment"/> 
    </id>   
    <property name="inquiryDate" type="date" column="inquiryDate"></property> 
    <property name="name" type="string" column="name"></property> 
    <property name="countryPreference" type="string" column="countryPreference"></property> 
    <property name="address" type="string" column="address"></property> 
    <property name="pnumberHome" type="string" column="pnumberHome"></property> 
    <property name="pnumberResi" type="string" column="pnumberResi"></property> 
    <property name="email" type="string" column="email"></property> 
    <property name="dob" type="date" column="dob"></property> 
    <property name="englishAblity" type="string" column="englishAblity"></property> 
    <property name="ieltsScore" type="string" column="ieltsScore"></property> 

    <property name="refusedVisa" type="string" column="refusedVisa"></property> 
    <property name="refusedCountry" type="string" column="refusedCountry"></property> 
    <property name="refusedTypeOfVisa" type="string" column="refusedTypeOfVisa"></property> 

    <property name="relativeAbroad" type="string" column="relativeAbroad"></property> 
    <property name="relativeCountry" type="string" column="relativeCountry"></property> 
    <property name="relativeStatus" type="string" column="relativeStatus"></property> 

    <property name="travellAbroad" type="string" column="travellAbroad"></property> 
    <property name="travleCountry" type="string" column="travleCountry"></property> 
    <property name="travelCountryVisaType" type="string" column="travelCountryVisaType"></property> 

    <property name="prefLevelCourse" type="string" column="prefLevelCourse"></property> 
    <property name="counselBy" type="string" column="counselBy"></property> 
    <property name="comeToKnow" type="string" column="comeToKnow"></property> 

    <property name="counselState" type="string" column="counselState"></property> 
    <property name="remarks" type="string" column="remarks"></property> 

    <set name="eduList" table="visaInquiry_education" cascade="persist" lazy="false"> 
     <key column="inquiryId"></key> 

     <many-to-many class="com.aems.beans.EducationQualification" column="educationId" unique="true"></many-to-many> 
    </set> 


    <set name="expList" table="visaInquiry_Experience" cascade="persist" lazy="false"> 
     <key column="inquiryId"></key> 
     <many-to-many class="com.aems.beans.Experience" column="expId" unique="true"></many-to-many> 
    </set> 

    <set name="childList" table="visaInquiry_Children" cascade="persist" lazy="false"> 
     <key column="inquiryId"></key> 
     <many-to-many class="com.`aems`.beans.Children" column="childId" unique="true"></many-to-many> 
    </set> 

    <many-to-one name="spouce" 
    class="com.aems.beans.Spouce" column="spouceId" cascade="persist" unique="true" lazy="false"/> 

Mit Kaskade = "persist" i den Fehler zum Zeitpunkt des Einsatzes erhalten. und mit cascade = "all" kein Problem zum Zeitpunkt der Einfügung, aber wenn ich lösche, Datensatz von nur einer Tabelle gelöscht werden. Alle zugehörigen Tabellen bleiben unverändert.

+0

Ich habe versucht, und es funktioniert gut für mich .. int id = Integer.parseInt (inquiryId); \t \t \t \t VisaAnfrage inq = (VisaAnfrage) session.load (VisaInquiry.class, id); \t \t \t \t session.delete (inq); \t \t \t \t session.flush(); –

Antwort

0

Ich habe dies versucht und es funktioniert gut für mich .. int id = Integer.parseInt (inquiryId); VisaInquiry inq = (VisaAnfrage) session.load (VisaInquiry.class, id); session.delete (inq); session.flush();

0

HQL löschen Abfragen (Massen löschen) nicht kaskadieren.

Nach JPA-Spezifikation (JSR338), Abschnitt 4.10:

Ein Löschvorgang gilt nur für Einheiten der angegebenen Klasse und ihre Unterklassen. Es kaskadiert nicht mit verbundenen Entitäten.

Wenn Sie also mit Kaskade löschen müssen, verwenden Sie Session.delete. Beachten Sie, dass Hibernate das Löschen von getrennten Entitäten unterstützt, was in der einfachen JPA nicht möglich ist.