JPA Projection query creates bloated constructor

286 Views Asked by At

When we use JPA projection queries , we end up creating different constructors based on the columns being selected.

This ends up as code smell since ideally we want the objects to be instantiated using builder / fluent API.

Also , SONAR reports such bloated constructors as violation .

Is there any other solution to achieve the JPA projection feature without compromising on code quality ?

1

There are 1 best solutions below

0
On

The JPA provider I use allows me to set the result class for a query (and not need to use the constructor JPQL notation), and then in the result class I can either provide a constructor taking the returned columns in the specified order, or a default constructor + setters, or a "put" method. As per this link http://www.datanucleus.org/products/accessplatform_5_0/jpa/jpql.html#Query_Result

Maybe your provider offers this also?