简体   繁体   English

休眠一对多关系级联删除

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

I am new to Hibernate so please guide me. 我是Hibernate的新手,请引导我。

I have 2 entities Companies & Employees. 我有2个实体公司和员工。 One Company should have many employees. 一个公司应该有很多员工。

Employees Hibernate Mapping File 员工休眠映射文件

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

Companies Hibernate Mapping File 公司休眠映射文件

<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 File 休眠配置文件

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

Simple POJO Classes. 简单的POJO类。

Employees.java Employees.java

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

Companies.java Companies.java

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

I want to delete Company Record from the companies table and all the employees from that company should be deleted. 我想从公司表中删除公司记录,并且应该删除该公司的所有员工。 But the problem I am facing is that Company Record is deleted by all the Employees Record relative to that Company aren't deleted. 但是我面临的问题是,与该公司相关的所有员工记录都不会删除公司记录。

Below is the Delete Code 以下是删除代码

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;
    }
}

You can rely on the database for cascading the DELETE statement, in which case you need to change the mapping to: 您可以依赖数据库来级联DELETE语句,在这种情况下,您需要将映射更改为:

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

If you don't want to change the mapping, you need to fetch the entity from the database and let Hibernate handle the children deletion: 如果您不想更改映射,则需要从数据库中获取实体,并让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