Hibernate One-to-Many关系级联删除

时间:2016-01-27 06:44:28

标签: java mysql hibernate hibernate-mapping hibernate-criteria

我是Hibernate的新手,所以请指导我。

我有2个实体公司&雇员。一家公司应该有很多员工。

员工Hibernate映射文件

<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映射文件

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

1 个答案:

答案 0 :(得分:0)

您可以依赖数据库来级联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;
}