Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 : https://stackoverflow.com/a/12742926/1383538

like image 643
Hayi Avatar asked Jan 24 '26 17:01

Hayi


1 Answers

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

like image 115
Neil Stockton Avatar answered Jan 26 '26 05:01

Neil Stockton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!