Hibernate One-to-Many relationship cascade delete
<p>I am new to Hibernate so please guide me.</p>

<p>I have 2 entities Companies &amp; Employees. One Company should have many employees.</p>

<p>Employees Hibernate Mapping File</p>

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

<p>Companies Hibernate Mapping File</p>

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

<p>Hibernate Configuration File</p>

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

        &lt;mapping resource="employees.hbm.xml"/&gt;
        &lt;mapping resource="companies.hbm.xml"/&gt;
    &lt;/session-factory&gt;
&lt;/hibernate-configuration&gt;
</code></pre>

<p>Simple POJO Classes.</p>

<p>Employees.java</p>

<pre><code>public class Employees {
    public Employees(){}
    private int empId;
    private int empCId;
    private String empName;
    private int empContact;
    //Getter &amp; Setter
}
</code></pre>

<p>Companies.java</p>

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

<p><strong>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.</strong></p>

<p>Below is the Delete Code</p>

<pre><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;
    }
}
</code></pre>