Tuesday, 27 August 2013

JPQL: How to "SELECT new Foo(null, null... someValue, ..)?

JPQL: How to "SELECT new Foo(null, null... someValue, ..)?

For example I have an entity
public class Foo {
private String col1;
private String col2;
private String col3;
private String col4;
//getters and setters
}
What I want to do is to select only col3 and col4. But I already have a
Foo constructor like below:
public Foo (String col1, String col2) {
this.col1 = col1;
this.col2 = col2;
}
Thus, I cannot have another constructor for col3 and col4 because it will
have the same signature.
What I am trying to accomplish so far is to make a complete constructor like:
public Foo (String col1, String col2, String col3, String col4) {
this.col1 = col1;
this.col2 = col2;
this.col3 = col3;
this.col4 = col4;
}
But when I try to do something like below in my query
SELECT new Foo(null, null, f.col3, f.col4)
FROM Foo f
I get
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of
subtree
Although when I try
SELECT new Foo(f.col1, f.col2, f.col3, f.col4)
FROM Foo f
It works as expected.

No comments:

Post a Comment