比如我有一個實體JPQL:如何「選擇新的Foo(NULL,NULL ... someValue中,..)
public class Foo {
private String col1;
private String col2;
private String col3;
private String col4;
//getters and setters
}
我想要做的是select
只有col3
和col4
。但是我已經有一個Foo
構造象下面這樣:
public Foo (String col1, String col2) {
this.col1 = col1;
this.col2 = col2;
}
因此,我不能再有一個構造函數col3
和col4
,因爲這將具有相同的簽名
我試圖到目前爲止完成是使完整構造,如:
public Foo (String col1, String col2, String col3, String col4) {
this.col1 = col1;
this.col2 = col2;
this.col3 = col3;
this.col4 = col4;
}
但是,當我嘗試做一些像下面我的查詢
SELECT new Foo(null, null, f.col3, f.col4)
FROM Foo f
我得到
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree
雖然當我嘗試
SELECT new Foo(f.col1, f.col2, f.col3, f.col4)
FROM Foo f
它按預期工作。
編輯:
我試圖
Select f.col3, col4..
及以下被拋出
org.springframework.dao.InvalidDataAccessApiUsageException: Cannot create TypedQuery for query with more than one return using requested result type [com.lala.entity.Foo]; nested exception is java.lang.IllegalArgumentException: Cannot create TypedQuery for query with more than one return using requested result type [com.lala.entity.Foo]
要選擇單個列,可以參考這裏 - http://stackoverflow.com/a/4536802/366964可能會有所幫助。 –