title: Insert into table with foreign key using Hibernate DAO method
body: <p>Here I'm trying to insert a row to a table which has a foreign key Using hibernate. But i get the following exception : "org.hibernate.MappingException: Unknown entity: com.xxx.model.Employee". How can i solve this issue? Following is my code : </p>
  
  <p>Employee.java:</p>
  
  <pre><code>@Entity
  @Table(name = "employee")
  @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  public class Employee {
  
  private static final long serialVersionUID = 1L;
  
  @Id
  @GeneratedValue
  @Column(name = "id")
  private long id;
  
  @Column(name = "emp_name")
  private String emp_name;
  
  @ManyToOne
  @JoinColumn(name="department_id")
  private Department department;
  
  @Column(name = "emp_id")
  private String emp_id;
  
  public long getId() {
      return id;
  }
  
  public void setId(long id) {
      this.id = id;
  }
  
  public String getEmp_name() {
      return floor_name;
  }
  
  public void setEmp_name(String emp_name) {
      this.emp_name = emp_name;
  }
  
  public Department getDepartment() {
      return department;
  }
  
  public void setDepartment(Department department) {
      this.department = department;
  }
  
  public String getEmp_id() {
      return emp_id;
  }
  
  public void setEmp_id(String emp_id) {
      this.emp_id = emp_id;
  }
  
  }
  </code></pre>
  
  <p>Department.java :</p>
  
  <pre><code>@Entity
  @Table(name = "department")
  @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  public class Department {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue
      @Column(name = "id")
      private long id;
  
      @Column(name = "dept_name")
      private String dept_name;
  
      public long getId() {
          return id;
      }
  
      public void setId(long id) {
          this.id = id;
      }
  
      public String getDept_name() {
          return dept_name;
      }
  
      public void setDept_name(String dept_name) {
          this.dept_name = dept_name;
      }
    }
  </code></pre>
  
  <p>To add new employee :</p>
  
  <pre><code>        Department department = null;
          department = departmentService.getDepartmentById(id);
  
          Employee employee = new Employee();
          employee.setEmp_name(emp_name);
          employee.setEmp_id(emp_id);
          employee.setDepartment(department);
          employeeService.addEmployee(employee);
  </code></pre>
  
  <p>In EmployeeDaoImpl :</p>
  
  <pre><code>@Override
  public boolean addEmployee(Employee employee) throws Exception {
      session = sessionFactory.openSession();
      tx = session.beginTransaction();
      session.save(employee);
      tx.commit();
      session.close();
      return false;
  }
  </code></pre>
  
  <p>In DepartmentDaoImpl :</p>
  
  <pre><code>@Override
  public Building getDepartmentById(long id) throws Exception {
      session = sessionFactory.openSession();
      Department department = (Department) session.load(Department.class, new Long(id));
      tx = session.getTransaction();
      session.beginTransaction();
      tx.commit();
      return department;
  }
  </code></pre>
  
  <p>config.xml:</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:context="http://www.springframework.org/schema/context"
      xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"&gt;
  
      &lt;context:component-scan base-package="com.xxx.controller" /&gt;
      &lt;mvc:annotation-driven /&gt;
  
      &lt;bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/test" /&gt;
          &lt;property name="username" value="root" /&gt;
          &lt;property name="password" value="root" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="annotatedClasses"&gt;
              &lt;list&gt;
                  &lt;value&gt;com.xxx.model.Employee&lt;/value&gt;
                  &lt;value&gt;com.xxx.model.Department&lt;/value&gt;
              &lt;/list&gt;
  
          &lt;/property&gt;
  
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="txManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="persistenceExceptionTranslationPostProcessor"
          class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /&gt;
  
      &lt;bean id="employeeDao" class="edu.am.amrita.trackapi.dao.EmployeeDaoImpl"&gt;&lt;/bean&gt;
      &lt;bean id="employeeService" class="edu.am.amrita.trackapi.services.EmployeeServiceImpl"&gt;&lt;/bean&gt;
  
      &lt;bean id="departmentDao" class="edu.am.amrita.trackapi.dao.DepartmentDaoImpl"&gt;&lt;/bean&gt;
      &lt;bean id="departmentService" class="edu.am.amrita.trackapi.services.DepartmentServiceImpl"&gt;&lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>