0

I have the entity following entities:
Employee extends Person
Company has List<Employee> (lazy load).

When I try to initialize the employee list with Hibernate.initialize(company.getEmployees()); I receive an error since hibernate doesn't understand that Employee is a Person.

Person.java

@Entity
@Table(name = "person")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Person implements java.io.Serializable {
    @Column(name = "person_id")
    protected Long personId;

    @Column(name = "name")
    protected String name;

    @Column(name = "age")
    protected String age;
}

Employee.java

@Entity
@Table(name = "employee")
@PrimaryKeyJoinColumn(name = "person_id")
public class Employee extends Person {
    @Column(name = "employee_number")
    private String number;

    @ManyToOne
    @JoinColumn(name = "company_id")
    private Company company;
}

Company.java

@Entity
@Table(name = "company")
public class Company implements java.io.Serializable {
    @Column(name = "company_id")
    protected String id;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "company", cascade = CascadeType.ALL)
    @OrderBy(clause = "name desc")
    protected List<Employee> employees = new ArrayList<Employee>();
}


Hibernate.initialize(company.getEmployees());

Exception:

org.postgresql.util.PSQLException: ERROR: column employee0_.name does not exist
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)

Is there any solution with lazy load for this case?

Marcelo Keiti
  • 1,200
  • 9
  • 10
  • I found this for you: http://stackoverflow.com/a/7827045/4531116 I know it is a bit old answer. – gabor.harsanyi Mar 03 '15 at 21:16
  • Thanks, this example is almost like mine except that my OneToMany is of subtype. I did some tests and realize that the problem is with `@OrderBy` clause that only supports direct properties of the collection elements. I will try to sort another way. – Marcelo Keiti Mar 03 '15 at 21:31

1 Answers1

0

There is certainly one to one relationship between Employee and Person. It makes sense by splitting the objects in Java but not in terms of table. I think Employee table should have a name and other columns defined in person table to simplify the design.

  • This is only an example to ilustrate. The real model is more complex and it makes sense to have this kind of relationship. – Marcelo Keiti Mar 03 '15 at 21:25