Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with one to many relationship

PremMay 8 2012 — edited May 8 2012
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);
			
		}



}
HibernateUtil.java
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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 5 2012
Added on May 8 2012
5 comments
3,228 views