I am using spring with JPA and trying to execute query using @query with SQL query.and trying to map the result to an object. I have different entity class and mapping to other
You can solve this issue and achieve the result by using projections by making your DTO an interface with getters for columns returned by the query. That's what I did when I faced the same issue.
For example in your case change StatsDTO
to interface like shown below, make sure to give alias for your columns if you are using '.' operator:
public interface StatsDTO {
Integer getUserCount();
Byte getTypeId();
Instant getModifiedAt();
}
Also in your query give alias for your columns like userCount, typeId, modifiedAt respectively so that it is mapped correctly.