1

I am new to hibernate. This is my master visaInquiry bean mapping that has many one-to-many and many-to many relation mapping through hibernate. Now to delete the record I am using this query

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

But problem is that it only delete records from the main table.And all the data on other tables remain as it is. I want to delete all the records from all related foreign table..Please Help?

    <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"/>

With cascade="persist" i get the error at time of insert. and with cascade="all" no problem at time of insert but when i delete, record from only one table get deleted. all related tables record remain as it is.

Rakesh Goswami
  • 205
  • 1
  • 4
  • 21
  • I have tries this and it works fine for me.. int id=Integer.parseInt(inquiryId); VisaInquiry inq = (VisaInquiry) session.load(VisaInquiry.class, id); session.delete(inq); session.flush(); – Rakesh Goswami Mar 26 '12 at 10:40

2 Answers2

0

HQL delete queries (bulk delete) do not cascade.

According to JPA specification (JSR338), section 4.10:

A delete operation only applies to entities of the specified class and its subclasses. It does not cascade to related entities.

So when you need to delete with cascade, use Session.delete. Note that Hibernate supports deletion of detached entities, which is not possible in plain JPA.

Jarekczek
  • 7,456
  • 3
  • 46
  • 66
0

I have tried this and it works fine for me.. int id=Integer.parseInt(inquiryId); VisaInquiry inq = (VisaInquiry) session.load(VisaInquiry.class, id); session.delete(inq); session.flush();

Rakesh Goswami
  • 205
  • 1
  • 4
  • 21