-->
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.  [ 5 posts ] 
Author Message
 Post subject: problems persisting primary/foreign keys in 1-N objects/rela
PostPosted: Tue Jun 22, 2010 2:33 am 
Newbie

Joined: Tue Jun 22, 2010 2:26 am
Posts: 4
i have two objects, Company and Employee. the relationship between these two objects are one to many (1-N); one Company has many Employees. when i instantiate a Company with a List of Employees, i cannot get the Company.id to persist to the Employee.companyId table/field. can someone tell me what i am doing wrong? my pojos are below.


Code:
package demo.bo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="employee")
public class Employee {

   @ManyToOne
   @JoinColumn(name="companyId")
   private Company company;
   
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private int id;
   private String firstName;
   private String lastName;
   
   public Employee() { }
   
   public Employee(String firstName, String lastName) {
      this.firstName = firstName;
      this.lastName = lastName;
   }

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getFirstName() {
      return firstName;
   }

   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }

   public String getLastName() {
      return lastName;
   }

   public void setLastName(String lastName) {
      this.lastName = lastName;
   }

   public Company getCompany() {
      return company;
   }

   public void setCompany(Company company) {
      this.company = company;
   }
}


Code:
package demo.bo;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name="company", uniqueConstraints={@UniqueConstraint(columnNames={"name"})})
public class Company {

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   @Column(name="id")
   private int id;
   private String name;
   
   @OneToMany(
         mappedBy="company",
         cascade={CascadeType.ALL},
         targetEntity=Employee.class,
         fetch=FetchType.EAGER)
   @JoinColumn(name="companyId")
   private List<Employee> employees;
   
   public Company() { }
   
   public Company(String name) {
      this.name = name;
   }
   
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   
   public void addEmployee(Employee employee) {
      List<Employee> employees = getEmployees();
      employees.add(employee);      
      employee.setCompany(this);
   }

   public List<Employee> getEmployees() {
      if(null == employees) {
         employees = new ArrayList<Employee>();
      }
      return employees;
   }

   public void setEmployees(List<Employee> employees) {
      this.employees = employees;
   }
   
}


Top
 Profile  
 
 Post subject: Re: problems persisting primary/foreign keys in 1-N objects/rela
PostPosted: Tue Jun 22, 2010 2:42 am 
Newbie

Joined: Tue Jun 22, 2010 2:26 am
Posts: 4
here is the logging output. i am using spring + jpa + hibernate + mysql.

Hibernate:
insert
into
company
(name)
values
(?)
Hibernate:
insert
into
employee
(companyId, firstName, lastName)
values
(?, ?, ?)
Jun 22, 2010 2:40:26 AM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1048, SQLState: 23000
Jun 22, 2010 2:40:26 AM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Column 'companyId' cannot be null


Top
 Profile  
 
 Post subject: Re: problems persisting primary/foreign keys in 1-N objects/rela
PostPosted: Tue Jun 22, 2010 2:43 am 
Newbie

Joined: Tue Jun 22, 2010 2:26 am
Posts: 4
here is my DDL for the tables.
Code:
drop table if exists employee;
drop table if exists company;

create table company (
   name varchar(100) unique,
   id mediumint not null auto_increment,
   primary key(id),
   index using btree(id)
);

create table employee(
   companyId mediumint not null,
   firstName varchar(100),
   lastName varchar(100),
   id mediumint not null auto_increment,
   primary key(id),
   index using btree(id),
   foreign key FK_COMPANY_ID (companyId) references company(id)
);



Top
 Profile  
 
 Post subject: Re: problems persisting primary/foreign keys in 1-N objects/rela
PostPosted: Tue Jun 22, 2010 3:11 am 
Beginner
Beginner

Joined: Mon Sep 14, 2009 9:29 am
Posts: 30
can you copy and paste your hbm file..i think the problem is with mapping..


Top
 Profile  
 
 Post subject: Re: problems persisting primary/foreign keys in 1-N objects/rela
PostPosted: Tue Jun 22, 2010 9:50 am 
Newbie

Joined: Tue Jun 22, 2010 2:26 am
Posts: 4
i don't have any hibernate XML mapping files at all. everything is annotation driven. at any rate, i found what i was doing wrong. my annotations were correct, but what i wasn't doing was setting the company field for the employee object.

for example, before everything worked, this is what i did.

Code:
Employee employee = new Employee("John", "Doe");
Company company = new Company("Microsoft");
company.getEmployees().add(employee);


to get things to work correctly, i did this (added one line).

Code:
Employee employee = new Employee("John", "Doe");
Company company = new Company("Microsoft");
company.getEmployees().add(employee);
employee.setCompany(company);


for a bidirectional one-to-many multiplicity, upon creation, each object must have a reference to one another.


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

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.