Skip to content
Advertisement

Load child collection DTOs in JPA DTO projection query

I’m using Java EE 7 with Java 8 and Hibernate (5.0.X) on Wildfly 10.1.0-Final, and I need to load a a JPQL query result into DTOs using projections, but I can’t find any documentation on how to load the child collection DTOs as well.

For instance, if I have following entities for User, Role, and Privilege:

JavaScript

And I want to use projections to load some query results into the following immutable DTOs (assume all have hashCode and equals implemented based on id):

JavaScript

What would the structure of a JPQL query look like to achieve this? I’m pretty sure I could get the job done by doing the joins and then processing the results into the DTO objects afterwards, something like this (to load the 50 newest users by ID):

JavaScript

The reconstruction would have to happen starting with PrivilegeDTO objects, then RoleDTO, finally UserDTO. This will allow for immutability because you need the PrivilegeDTO objects when you build the RoleDTO objects, or you would have to add them later, meaning RoleDTO is not immutable.

It’d be a fun exercise in Streams, but I’d much prefer to be able to just have this built from the query, it seems like it would have to be faster. Is that even possible?

Thanks a lot!

Advertisement

Answer

Hi Morgan short answer is no, you can’t built from the query because you cant map JPQL to DTO Collections fields. Here it’s a question related with that JPQL: Receiving a Collection in a Constructor Expression

Anyway you could try an approach with Spring projections using spel https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query.spel-expressions

But i think the right solution is just use manual mapping like is explained in this answer https://stackoverflow.com/a/45934668/3449039

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement