例如我有一个实体
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]