2012-03-02 54 views
0

我在查詢時遇到問題,但僅在HSQLDB下。我們使用Oracle進行生產數據庫和HSQL進行自動化集成測試。這是我的主要目標:JPA /休眠/ HSQLDB查詢但未分配子對象

@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看起來是這樣的:

@Entity 
@Table(name="STUDENTRACE") 
@org.hibernate.annotations.Proxy(lazy=false) 
public class StudentRace implements Serializable { 
... 
    @Column(name="STUDENTID") 
    private Integer studentid; 
... 
} 

我的JPA查詢是這樣的:

entityManager.createQuery("select distinct s from Student s left join fetch s.races "+ 
    "where s.schoolNumber = :schoolNumber"); 

我知道我必須在HSQLDB數據庫中正確的數據 - 我可以手動進行查詢並查看數據。然而,學生對象總是有「種族」爲空。正如我所說,這個查詢在Oracle中工作正常。是否有某種我缺少的HSQLDB設置?

編輯:喜歡這個?學生:

@OneToMany(fetch=FetchType.LAZY, mappedBy="id") 
private Set<StudentRace> races; 

在StudentRace:

@ManyToOne 
@JoinColumn(name="STUDENTID") 
private Student student; 

仍然沒有運氣。 「種族」元素仍爲空。

回答

1

你的映射是不正確的,因爲STUDENTRACE.STUDENTID列映射了兩次:在StudentStudentRace列一次作爲JoinColumn,以及一次。

要麼進行關聯單向並刪除StudentRace的studentid字段,或使它雙向的,並具有在一個StudentRace字段Student student,映射爲一個多對一,如圖this example from the Hibernate documentation