Youssef Youssef - 4 months ago 8
Java Question

Query with nested constructor

When i try this query with nested constructor with my custum DTO.

select new DTO.foo ( new DTO.boo( ... ) , new DTO.boo( ... ) ) from Foo


I got this error :

org.hibernate.hql.internal.ast.QuerySyntaxException:
unexpected token: new near line 1, column 23 [ select new DTO.foo ( new DTO.boo( ... ) , .....


SOLUTION

Since we cannot have a constructor within a constructor because it's illegal in
JPQL
We resolve this issue by following the same approach over here : http://stackoverflow.com/a/12742926/1383538

Answer

That is illegal JPQL, as per the JPQL BNF notation

constructor_expression ::= NEW constructor_name ( constructor_item {, constructor_item}* )
constructor_item ::= single_valued_path_expression | scalar_expression | aggregate_expression |
    identification_variable

You cannot have a constructor within a constructor (i.e constructor_item cannot be a constructor_expression). See the JPA spec