title: unable to store data into child table through parent table in hibernate one to many Bi-directional Process
body: <p>I am trying to store the data into EMPLOYEE(child) table through DEPARTMENT(parent) table and dispaly it using the hibernate one to many bi-directional process.</p>
  
  <p>After the transaction i am able to diaply the data in employee including the data what i want to enter but i am not able to see that data in database(POSTGRES). Please help us and thanks in advance.</p>
  
  <p>I have to two tables Department(Parent) and employee(child refferring dep_id in department).</p>
  
  <pre><code>CREATE TABLE department
  (
  department_id character varying(10) NOT NULL,
  department_name character varying(20),
  CONSTRAINT "pk_departmentId" PRIMARY KEY (department_id)
  ) 
  
  CREATE TABLE employee
  (
  emp_id character varying(10) NOT NULL,
  emp_name character varying(20),
  department_id character varying(10),
  CONSTRAINT "pk_empId" PRIMARY KEY (emp_id),
  CONSTRAINT "fk_empDept" FOREIGN KEY (department_id) REFERENCES department (department_id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION
  )
  </code></pre>
  
  <p>The respective hbm files are:</p>
  
  <p><strong>*<em>DEPARTMENT</em>*</strong></p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
  &lt;!-- Generated Oct 9, 2012 3:01:23 PM by Hibernate Tools 3.2.1.GA --&gt;
  &lt;hibernate-mapping&gt;
      &lt;class name="tablebeans.Department" table="department" schema="public"&gt;
          &lt;id name="departmentId" type="string"&gt;
              &lt;column name="department_id" length="10" /&gt;
              &lt;generator class="assigned" /&gt;
          &lt;/id&gt;
          &lt;property name="departmentName" type="string"&gt;
              &lt;column name="department_name" length="20" /&gt;
          &lt;/property&gt;
          &lt;set name="employees" table="employee" inverse="true" lazy="true" fetch="select"&gt;
              &lt;key&gt;
                  &lt;column name="department_id" length="10" /&gt;
              &lt;/key&gt;
              &lt;one-to-many class="tablebeans.Employee" /&gt;
          &lt;/set&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>*<strong><em>EMPLOYEE</em>*</strong></p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
  &lt;!-- Generated Oct 9, 2012 3:01:23 PM by Hibernate Tools 3.2.1.GA --&gt;
  &lt;hibernate-mapping&gt;
      &lt;class name="tablebeans.Employee" table="employee" schema="public"&gt;
          &lt;id name="empId" type="string"&gt;
              &lt;column name="emp_id" length="10" /&gt;
              &lt;generator class="assigned" /&gt;
          &lt;/id&gt;
          &lt;many-to-one name="department" class="tablebeans.Department" fetch="select"&gt;
              &lt;column name="department_id" length="10" /&gt;
          &lt;/many-to-one&gt;
          &lt;property name="empName" type="string"&gt;
              &lt;column name="emp_name" length="20" /&gt;
          &lt;/property&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>The POJO classes are :</p>
  
  <pre><code>package tablebeans;
  // Generated Oct 9, 2012 3:01:23 PM by Hibernate Tools 3.2.1.GA
  
  
  
  
  import java.util.HashSet;
  import java.util.Set;
  
  /**
   * Department generated by hbm2java
   */
  public class Department  implements java.io.Serializable {
  
       private String departmentId;
       private String departmentName;
       private Set employees = new HashSet(0);
  
      public Department() {
      }
  
      public Department(String departmentId) {
          this.departmentId = departmentId;
      }
      public Department(String departmentId, String departmentName, Set employees) {
         this.departmentId = departmentId;
         this.departmentName = departmentName;
         this.employees = employees;
      }
  
      public String getDepartmentId() {
          return this.departmentId;
      }
  
      public void setDepartmentId(String departmentId) {
          this.departmentId = departmentId;
      }
      public String getDepartmentName() {
          return this.departmentName;
      }
  
      public void setDepartmentName(String departmentName) {
          this.departmentName = departmentName;
      }
      public Set getEmployees() {
          return this.employees;
      }
  
      public void setEmployees(Set employees) {
          this.employees = employees;
      }
  }
  
  
  package tablebeans;
  // Generated Oct 9, 2012 3:01:23 PM by Hibernate Tools 3.2.1.GA
  
  
  
  
  
  /**
   * Employee generated by hbm2java
   */
  public class Employee  implements java.io.Serializable {
  
       private String empId;
       private Department department;
       private String empName;
  
      public Employee() {
      }
  
      public Employee(String empId) {
          this.empId = empId;
      }
      public Employee(String empId, Department department, String empName) {
         this.empId = empId;
         this.department = department;
         this.empName = empName;
      }
  
      public String getEmpId() {
          return this.empId;
      }
  
      public void setEmpId(String empId) {
          this.empId = empId;
      }
      public Department getDepartment() {
          return this.department;
      }
  
      public void setDepartment(Department department) {
          this.department = department;
      }
      public String getEmpName() {
          return this.empName;
      }
  
      public void setEmpName(String empName) {
          this.empName = empName;
      }
  }
  </code></pre>
  
  <p>And the code from which i am trying to store the data into the EMPLOYEE table through DEPARTMENT table and displaying it.  </p>
  
  <pre><code> tx = session.beginTransaction();
  
              Department department=new Department();
              department.setDepartmentId("5");
              department.setDepartmentName("Nielson");
  
              Employee employee = new Employee();
              employee.setEmpId("4");
              employee.setEmpName("phani");
              employee.setDepartment(department);
  
              Set empset = new HashSet();
              empset.add(employee);
              department.setEmployees(empset);
  
              session.save(department);
              tx.commit();
  
              //Query query = session.createQuery("insert into Department(departmentId,departmentName)"+"select departmentId,departmentName from Employee where departmentId = 5");
              //int update = query.executeUpdate();
  
              Query query1=session.createQuery("from Department");
              List list1=query1.list();
              for(int i=0;i&lt;list1.size();i++)
                 {
                  System.out.println("In for");
                  Department department1 =(Department) list1.get(i);
                  System.out.println("the ID is "+department1.getDepartmentId());
                  System.out.println("the dept name is "+department1.getDepartmentName());
                  Set st = (Set) department1.getEmployees();
                  Iterator ite = st.iterator();
                  while(ite.hasNext())
                  {
                      employee = (Employee) ite.next();
                      System.out.println("the emp name is "+employee.getEmpName());
                  }
  
                }
  </code></pre>
  
  <p>Here i am able to display the newly entered record into the employee table but that data is not resembling in the database table. I am using the Postgres.</p>