1 Reply Latest reply: May 7, 2012 9:16 AM by Cdelahun-Oracle RSS

    Problem with one to many relationship

    Prem
      Hi I am implementing JPA hibernate simple application using one to many relationship.

      Relation ship is Comapny (1)----------- Department(*)

      Company.java is as follow :
      package com.web.pojo;
      
      import java.util.ArrayList;
      import java.util.List;
      
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.JoinTable;
      import javax.persistence.OneToMany;
      
      import org.hibernate.annotations.Cascade;
      
      
      
      @Entity
      public class Company {
           
            
           @Id
           @GeneratedValue
           private int compId;
           private String companyName;
           
           @OneToMany(fetch=FetchType.LAZY)
           @JoinColumn(name="compId")
           @Cascade(org.hibernate.annotations.CascadeType.ALL)
           private List <Department> listOfDepartment = new ArrayList<Department>();
           
           
           public List<Department> getListOfDepartment() {
                return listOfDepartment;
           }
           public void setListOfDepartment(List<Department> listOfDepartment) {
                this.listOfDepartment = listOfDepartment;
           }
           public int getCompanyId() {
                return compId;
           }
           public void setCompanyId(int companyId) {
                this.compId = companyId;
           }
           public String getCompanyName() {
                return companyName;
           }
           public void setCompanyName(String companyName) {
                this.companyName = companyName;
           }
      
      }
      Department.java is as follow :
      package com.web.pojo;
      
      import java.util.ArrayList;
      import java.util.List;
      
      import javax.persistence.CascadeType;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.JoinTable;
      import javax.persistence.ManyToMany;
      import javax.persistence.ManyToOne;
      import javax.persistence.OneToMany;
      
      import org.hibernate.annotations.Cascade;
      
      
      @Entity
      public class Department {
           
           @Id
           @GeneratedValue
           private int deptId;
           private String deptName;
      
           @ManyToOne
           @JoinColumn(name="compId")
           private Company company;
           
           @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
           private List<Employee>listofEmployee = new ArrayList<Employee>();
           
           
           public int getDeptId() {
                return deptId;
           }
           public void setDeptId(int deptId) {
                this.deptId = deptId;
           }
           public String getDeptName() {
                return deptName;
           }
           public void setDeptName(String deptName) {
                this.deptName = deptName;
           }
           public Company getCompany() {
                return company;
           }
           public void setCompany(Company company) {
                this.company = company;
           }
           public List<Employee> getListofEmployee() {
                return listofEmployee;
           }
           public void setListofEmployee(List<Employee> listofEmployee) {
                this.listofEmployee = listofEmployee;
           }
           
      
      }
      CompanyDAO.java is follow
      package com.web.hibernatetier;
      
      import java.util.ArrayList;
      import java.util.List;
      
      import com.web.HibernateUtil;
      import com.web.pojo.Company;
      import com.web.pojo.Department;
      
      public class CompanyDAO {
           
                     
      
                    public void addCompany(Company company) {
                     
                     HibernateUtil.startHibernateSession();
                     HibernateUtil.session.save(company);
                     HibernateUtil.endHibernateSession();
                     
                }
                
                public void addDepartmentToCompany(Department department,int compId) {
                     
                     HibernateUtil.startHibernateSession();
                     Company company = (Company) HibernateUtil.session.get(Company.class, compId);
                     company.getListOfDepartment().add(department);
                     Department dept = company.getListOfDepartment().get(0);
                     System.out.println(dept.getCompany().getCompanyName());
                     HibernateUtil.endHibernateSession();
                }
                
                
                
                public static void main(String[] args) {
                     
                     
                               CompanyDAO dao = new CompanyDAO();
      
                              Company company = new Company();
                     company.setCompanyName("ABC");
      
                               dao.addCompany(company);
                     
      
                     
                     dao.addCompany(company);
                
                     // Department Transactions
      
                     
                     Department department = new Department();
                     department.setDeptName("Development");
                     
                     dao.addDepartmentToCompany(department, 1);
                     
                }
      
      
      
      }
      What I am doing is , I am adding new department to existing company. After execution above code

      Table created are :
      1. Company table with id and name
      2. Department table with deptId , deptName , compId.

      so at the time adding department , it does not threw any exception but updates records in department as
      1(Id),Development(DeptName),null(compId) .

      I am not getting why it is not updating compId column. Please help.
        • 1. Re: Problem with one to many relationship
          Cdelahun-Oracle
          Hello,

          I cannot tell what your HibernateUtil object does, but I suspect from the name you are not using a JPA provider such as TopLink or EclipseLink - this is a product forum for TopLink and annotations like @Cascade are not portable JPA annotations. That asside, I would not expect this to work well in any JPA provider because you have set up two uni-directional relationships that both use the same Department.compId database field. In EclipseLink/TopLink you should get an exception stating you have multiple writable mappings to this field.

          What I believe you intended was to setup 1 bidirectional relationship. To do this, instead of using the @JoinColumn on the OneToMany relaitonhips, remove that annotation and change the OneToMany to:
          @OneToMany(fetch=FetchType.LAZY, mappedby="company")

          You will also want to set the back pointer when you add a Department to Company so that what you have in your object model matches what you want to have in the database.