簡體   English   中英

休眠一對多關系級聯刪除

[英]Hibernate One-to-Many relationship cascade delete

我是Hibernate的新手,請引導我。

我有2個實體公司和員工。 一個公司應該有很多員工。

員工休眠映射文件

<hibernate-mapping>
   <class name="com.hibernate.demo.Employees" table="employees">
      <meta attribute="class-description">
         This class contains the employee detail. 
      </meta>
      <id name="empId" type="int" column="emp_id">
         <generator class="native"/>
      </id>
      <property name="empCId" column="emp_cid" type="int"/>
      <property name="empName" column="emp_name" type="string"/>
      <property name="empContact" column="emp_contact" type="int"/>
   </class>
</hibernate-mapping>

公司休眠映射文件

<hibernate-mapping>
   <class name="com.hibernate.demo.Companies" table="companies" >
      <meta attribute="class-description">
         This class contains the companies detail. 
      </meta>
      <id name="compId" type="int" column="comp_id">
         <generator class="native"/>
      </id>
      <set name="employees" cascade="all" >
         <key column="emp_cid"/>
         <one-to-many class="com.hibernate.demo.Employees" />
      </set>
      <property name="compName" column="comp_name" type="string"/>
      <property name="compCity" column="comp_city" type="string"/>
   </class>
</hibernate-mapping>

休眠配置文件

<hibernate-configuration>
   <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedbdemo</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">knowarth</property>
        <property name="show_sql">true</property>       
        <property name="format_sql">true</property>     
        <property name="hbm2ddl.auto">update</property>     

        <mapping resource="employees.hbm.xml"/>
        <mapping resource="companies.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

簡單的POJO類。

Employees.java

public class Employees {
    public Employees(){}
    private int empId;
    private int empCId;
    private String empName;
    private int empContact;
    //Getter & Setter
}

Companies.java

public class Companies {
    public Companies(){}
    private int compId;
    private String compName;
    private String compCity;
    private Set<Employees> employees;
    //Getter & Setter
}

我想從公司表中刪除公司記錄,並且應該刪除該公司的所有員工。 但是我面臨的問題是,與該公司相關的所有員工記錄都不會刪除公司記錄。

以下是刪除代碼

public class CompanyDao {
    Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
    SessionFactory sf = cfg.buildSessionFactory();
    Session session = sf.openSession();
    Companies comp = new Companies();
    Scanner compSc = new Scanner(System.in);

    public void deleteComp(){
        session.beginTransaction();
        System.out.println("Enter Company ID to delete it");
        int cmp_id = compSc.nextInt();
        Companies company = new Companies();
        company.setCompId(cmp_id);  
        session.delete(company);        
        session.getTransaction().commit();
        return;
    }
}

您可以依賴數據庫來級聯DELETE語句,在這種情況下,您需要將映射更改為:

 <set name="employees" cascade="all" inverse="true" >
     <key column="emp_cid" on-delete="cascade" />
     <one-to-many class="com.hibernate.demo.Employees" />
 </set>

如果您不想更改映射,則需要從數據庫中獲取實體,並讓Hibernate處理子項刪除:

public void deleteComp(){
    session.beginTransaction();
    System.out.println("Enter Company ID to delete it");
    int cmp_id = compSc.nextInt();
    Companies company = session.get(Companies.class, cmp_id); 
    session.delete(company);        
    session.getTransaction().commit();
    return;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM