I am having trouble with this query but ONLY under HSQLDB. We use Oracle for production database and HSQL for automated integration tests. Here’s my main object:
@Entity
@Table(name="STUDENTS")
@org.hibernate.annotations.Proxy(lazy=false)
public class Student implements Serializable {
...
@OneToMany(fetch=FetchType.LAZY)
@JoinColumn(name="STUDENTID",referencedColumnName="ID")
private Set<StudentRace> races;
...
}
StudentRace looks like this:
@Entity
@Table(name="STUDENTRACE")
@org.hibernate.annotations.Proxy(lazy=false)
public class StudentRace implements Serializable {
...
@Column(name="STUDENTID")
private Integer studentid;
...
}
And my JPA query is like this:
entityManager.createQuery("select distinct s from Student s left join fetch s.races "+
"where s.schoolNumber = :schoolNumber");
I know I have correct data in the HSQLDB database – I can manually do a query and see the data. Yet the Student objects always have “races” as null. As I said, this query works fine in Oracle. Is there some kind of HSQLDB setup thing I’m missing?
Edit: Like this? In Student:
@OneToMany(fetch=FetchType.LAZY, mappedBy="id")
private Set<StudentRace> races;
In StudentRace:
@ManyToOne
@JoinColumn(name="STUDENTID")
private Student student;
Still no luck. The “races” element is still null.
Your mapping is incorrect, because the
STUDENTRACE.STUDENTIDcolumn is mapped twice: once as a JoinColumn inStudent, and once as a column inStudentRace.Either make the association unidirectional and remove the studentid field in
StudentRace, or make it bidirectional, and have aStudent studentfield inStudentRace, mapped as a ManyToOne, as shown in this example from the Hibernate documentation.