title: How to use Hibernate annotations @ManyToOne and @OneToMany for associations
body: <p>I am learning Spring, Hibernate, Maven by using this tutorial: <a href="http://www.giantflyingsaucer.com/blog/?p=2934">Chad Lung: A project using Netbeans 7, JUnit, Maven, HSQLDB, Spring and Hibernate</a>. It works ok but I need to make one-to-many relationship (one Employee have many Tasks). I have tried many examples but still can't get idea how to make my code work:</p>
  
  <p><strong>Employee.java:</strong></p>
  
  <pre><code>package com.giantflyingsaucer.simplespringhibernate.entity;
  
  import javax.persistence.*;
  import java.io.Serializable;
  import java.util.List;
  
  @Entity
  @Table(name = "Employees")
  public class Employee implements Serializable {
  
      private Integer employeeId;
      private List&lt;Task&gt; tasks;
  
      @Id
      @Column(name = "idEmployees", nullable=false)
      public Integer getEmployeeId() {
          return this.employeeId;
      }
  
      public void setEmployeeId(Integer employeeId) {
          this.employeeId = employeeId;
      }
  
      @OneToMany(fetch = FetchType.LAZY)
      @JoinColumn(name="idEmployees")
      public List&lt;Task&gt; getTasks() {
          return tasks;
      }
  }
  </code></pre>
  
  <p><strong>Task.java:</strong></p>
  
  <pre><code>package com.giantflyingsaucer.simplespringhibernate.entity;
  
  import javax.persistence.*;
  import java.io.Serializable;
  
  @Entity
  @Table(name = "Tasks")
  public class Task implements Serializable {
  
      private Integer taskId;
      private Employee employee;
  
  
      @Id
      @Column(name = "idTasks", nullable=false)
      public Integer getTaskId() {
          return this.taskId;
      }
  
      public void setTaskId(Integer taskId) {
          this.taskId = taskId;
      }
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "TasksIdEmployees")
      public Employee getEmployee() {return employee;}
  
  }
  </code></pre>
  
  <p><strong>db-config.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  "&gt;
  &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close"&gt;
  
      &lt;property name="driverClass"&gt;
          &lt;value&gt;${jdbc.driver.className}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="jdbcUrl"&gt;
          &lt;value&gt;${jdbc.url}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="user"&gt;
          &lt;value&gt;${jdbc.username}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="password"&gt;
          &lt;value&gt;${jdbc.password}&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="dataSource"&gt;
          &lt;ref bean="dataSource" /&gt;
      &lt;/property&gt;
      &lt;property name="packagesToScan" value="com.giantflyingsaucer.simplespringhibernate.entity" /&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;${jdbc.hibernate.dialect}&lt;/prop&gt;
              &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;false&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="transactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory"&gt;
          &lt;ref bean="sessionFactory" /&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;tx:annotation-driven /&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>MySQL tables:</strong></p>
  
  <pre><code>CREATE TABLE employees (
  `idEmployees` int(11) NOT NULL,
  PRIMARY KEY (`idEmployees`)
  );
  
  CREATE TABLE tasks (
  `idTasks` int(11) NOT NULL,
  `TasksIdEmployees` int(11) DEFAULT NULL,
  PRIMARY KEY (`idTasks`),
  KEY `FkTasksEmployees_idx` (`TasksIdEmployees`),
  CONSTRAINT `FkTasksEmployees` FOREIGN KEY (`TasksIdEmployees`) REFERENCES `employees`   (`idEmployees`) ON DELETE NO ACTION ON UPDATE NO ACTION
  );
  </code></pre>
  
  <p>Thanks a lot!</p>
  
  <p>I found an answer by autogenerating Mapping files and POJOs in NetBeans:</p>
  
  <pre><code>// Employee.java:
      @OneToMany(fetch = FetchType.LAZY, mappedBy = "employees")
      public List&lt;Task&gt; getTasks() {
          return this.tasks;
      }
  
      public void setTasks(List&lt;Task&gt; tasks) {
          this.tasks = tasks;
      }
  
  // Task.java:
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "TasksIdEmployees")
  public Employee getEmployees() {
      return this.employee;
  }
  
  public void setEmployees(Employee employee) {
      this.employee = employee;
  }
  </code></pre>