-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Problem with one to many relationship
PostPosted: Tue May 08, 2012 2:50 am 
Newbie

Joined: Fri Aug 15, 2008 9:09 am
Posts: 12
Hi I am implementing JPA hibernate simple application using one to many relationship.

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

Company.java is as follow :

Code:

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,mappedBy="company")
   @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 :

Code:

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

Code:
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);
         
      }



}



HibernateUtil.java

Code:

public class HibernateUtil {
   
   private static SessionFactory sessionFactory ;
   public static Session session;
   
   private HibernateUtil() {
      
   }
   
   private static SessionFactory getSessionFactory() {
      
      if(sessionFactory==null) {
          sessionFactory = new Configuration().configure().buildSessionFactory();
      }
      return sessionFactory;
      
   }
   
   
   public static void startHibernateSession() {
      
      sessionFactory = getSessionFactory();
      session = sessionFactory.openSession();
      session.beginTransaction();
   }
   
   
   public static void endHibernateSession() {
      session.getTransaction().commit();
      session.close();
   }
   

}



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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.