Q:

How to Convert a JQPL Query into a DTO?

I need to know if it's possible for me to convert my JQPL query result into a DTO.
The query result is a Array of Arrays like this Json:
[
  [
     ModuleID: number,
     ModuleName: string,
     ToolId: number,
     ToolName: string,
     Enabled: boolean
  ],
]

And I want to convert into this DTO:
public class ModuleDTO {

private Long ModuleID;
private String ModuleName;
private List<ToolsDTO> Tools;

}

public class ToolsDTO {

private Long ToolId;
private String ToolName;
private Boolean Enabled; 

}

You can see that the last three are children of the module, that means that in the search there may be repeated modules, but all children must be within the same list.

A:

This is a perfect use case for Blaze-Persistence Entity Views.
Blaze-Persitence is a query builder on top of JPA which supports many of the advanced DBMS features on top of the JPA model. I created Entity Views on top of it to allow easy mapping between JPA models and custom interface defined models, something like Spring Data Projections on steroids. The idea is that you define your target structure the way you like and map attributes(getters) via JPQL expressions to the entity model. Since the attribute name is used as default mapping, you mostly don't need explicit mappings as 80% of the use cases is to have DTOs that are a subset of the entity model.
You didn't specify an entity model so I'm going to assume some things here. A mapping could look as simple as the following
@EntityView(Module.class)
interface ModuleDTO {
    @IdMapping
    Long getModuleId();
    String getModuleName();
    List<ToolsDTO> getTools();
}

@EntityView(Tools.class)
interface ToolsDTO {
    @IdMapping
    Long getToolId();
    String getToolName();
    Boolean getEnabled();
}

Querying is a matter of applying the entity view to a query, the simplest being just a query by id.
ModuleDTO dto = entityViewManager.find(entityManager, ModuleDTO.class, id);
But the Spring Data integration allows you to use it almost like Spring Data Projections: https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features
It will only fetch the mappings that you tell it to fetch

