I am new to Hibernate so please guide me.
I have 2 entities Companies & Employees. 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.
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
}
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:
 <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:
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With