Announcement Announcement Module
No announcement yet.
JPA - Not loading relationship objects Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • JPA - Not loading relationship objects

    I have an employee table with employee_id and supervisor_employee_id columns. The supervisor is also a record in the table.

    The mapping object looks something like:
    public class Employee implements Serializable {
      @GeneratedValue(strategy = GenerationType.AUTO)
      private String employeeId;
      @JoinColumn(name="supervisor_employee_id", nullable=true, insertable=false, updatable=false)
      private Employee supervisor;
      @OneToMany(mappedBy="supervisor"/*, fetch=FetchType.LAZY*/)
      private Set<Employee> employees = new HashSet<Employee>(0); // direct reports
    The DAO looks something like (extraneus code removed):
    public class EmployeeDaoImpl implements EmployeeDao {
      private EntityManager em;
      public List<Employee> findEmployeesForName(...snip...) {
          TypedQuery<Employee> query = em.createQuery(sql, Employee.class);
          emps = query.getResultList();
          ... code here to touch every supervisor/directReportEmployee for each relevant Employee object returned, so that I can use lazy fetching...
         return emps;
    The persistence file is:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.0" 
        xmlns             =""
        xmlns:xsi         =""
       <!-- SPRING version -->
       <persistence-unit name="kronosUnit" transaction-type="RESOURCE_LOCAL">
    And the context configuration contains:
      <ctx:annotation-config />
      <ctx:component-scan base-package="packageForJpaObjects" />
      <ctx:component-scan base-package="packageForDao" />
      <bean id="employeeDao"                      class="packageForDao.EmployeeDaoImpl" />
      <bean id="myEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation"   value="classpath*:persistence-mine.xml"/>
        <property name="dataSource"               ref="myDataSource" />
        <property name="jpaVendorAdapter">
          <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform"         value="org.hibernate.dialect.SQLServerDialect"/>
            <property name="showSql"                  value="false" />
            <property name="generateDdl"              value="false" />
      <bean class="" />
      <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
      <tx:annotation-driven transaction-manager="myTransactionManager" proxy-target-class="false"/>
      <bean id="myTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="myEntityManagerFactory" />
    The employee objects get loaded fine. But instead of getting a supervisor or direct report object... those objects come back with nulls in all their values - if I use lazy loading, even when I touch the records inside of the transaction. If I try to use eager fetching... I get javax.persistence.EntityNotFoundException: Unable to find packageName.Employee with id NULL even though I verified that the supervisor_employee_id value is NOT null and further, it has the correct value for employee_id in the supervisor's employee record.

    What is curious is that I had a virtually identical set of code in another app (the only difference where the database column names)... and that one never had any problems.

    Where did I go wrong?


    Something else that I noticed... the employee_id and supervisor_employee_id columns are nvarchar, not int. This was never a problem in the previous app, but I saw that it is now complaining that an Identity column should be an int. How would I modify the annotations for employeeId so that the column is still treated as the primary key (and supervisor and direct reports map back to it) while still being treated as String/nvarchar?