2017-10-10 91 views
1

我創建了以下類,即教師和學生。過濾器無法處理一對多關係的JPQL查詢

Java代碼:學生

@Entity 
@Table(name="Student") 

public class Student { 

    @Id 
    @Column(name = "cmp_id") 
    private String cmpId; 

    @OneToOne(cascade = CascadeType.REFRESH) 
    @JoinColumn(name = "teacherid", referencedColumnName = "teacherid") 
    private Teacher teacher; 

    @OneToOne 
    @JoinColumn(name = "typeid", referencedColumnName = "typeid") 
    private Type type; 
} 

Java代碼:教師

@Entity 
@Table(name="Teacher") 
public class Teacher { 

    @Id 
    @Column(name = "teacherid") 
    private String teacherid; 

    @Column(name = "teachername") 
    private String teachername; 

    @OneToMany(mappedBy = "Teacher", fetch = FetchType.EAGER, cascade = CascadeType.ALL) 
    @OrderBy("cmpSequence ASC") 
    private List<Student> std; 
} 

Java代碼:類型

@Entity 
@Table(name="Type") 
public class Type{ 

    @Id 
    @Column(name = "typeid") 
    private Integer typeid; 

    @Column(name = "uitype") 
    private String uitype;  
} 

教師是父類和學生是孩子class.I希望所有教師和他們的學生與過濾typeid是1,但它不起作用。

以下是JPQL查詢,我創建了:

SELECT teacher FROM Teacher teacher WHERE teacher.std.typeid = 1

它給了我以下錯誤:

java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: Exception Description: Problem compiling query.The state field path 'teacher.std.typeid' cannot be resolved to a valid type.

+1

的可能的複製[JPQL:狀態字段路徑不能解析爲一個有效的類型(https://stackoverflow.com/questions/20056847/jpql-the-state -field-path-can-be-resolved-to-a-valid-type) – Nikolas

+0

,因爲你的JPQL是無稽之談。您不能在WHERE子句中通過它們引用多值字段,您必須執行顯式JOIN。但是,然後任何基本的JPA文檔將包括這 – DN1

回答

3

您的查詢是錯誤的。你有Teacher它有std(Student),std有類型(Type),類型有typeid字段。因此,您的查詢應該看起來像如下:

SELECT teacher FROM Teacher teacher 
JOIN FETCH teacher.std AS s 
INNER JOIN s.type as t 
WHERE t.typeid = 1 
+0

我做了更改,但沒有工作。 – dny

+0

添加類別代碼 –

+0

我添加了代碼類型 – dny