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
If you are still looking for an answer, here is how I did
@Data
@NoArgsConstructor
@AllArgsConstructor
public class StatsDTO {
private Integer userCount;
private Byte typeId;
private Instant modifiedAt;
}
and your query should be like this
public interface UserRepository extends CrudRepository {
@Query(value = "select new com.example.package.StatsDTO(count(type_id) userCount, typeId, modifiedAt from "
+ "UserCampaignObjective where campId = ?1 group by objectiveTypeId,modifiedAt")
List getStatsDTO(Long camp_id);
}
make sure you have a constructor present in StatsDTO to map all fields.