2012-09-07 118 views
0

我在SQL如下:JPA多個實體集合查詢

SELECT * FROM table2 t2, table1 t1 WHERE t1.id=1 AND t2.t1_id IN(1, 2); 

我會怎麼做,在JPA?

我想:

SELECT t2 FROM Table2 t2, Table1 t1 WHERE t1.id = :t1_id AND t2.t1 IN (t1.t1Collection) 

隨着僞類:

Table1 
    private int id; 
    private Collection<Table1> t1Collection; 

Table2 
    private int t2; 
    private Table1 t1_id; 

,但我得到:

org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query 

回答

3

嘗試elements關鍵字:

SELECT t2 FROM Table2 t2, Table1 t1 WHERE t1.id = :t1_id AND t2.t1 IN elements(t1.t1Collection) 
+0

這就是它,謝謝! – jsurls

+5

只是澄清一點:'IN elements'是Hibernate特有的語法。 JPA推論是''的成員: –