I have a slight problem at a glance:
entityManager()
.createNativeQuery("select count(*) as total, select sum(field) as total_sum ... blabla")
And I want to write the selection result in POJO, for example:
public class AggregateStatsDto {
private int total;
private long totalSum;
}
What is the best way to achieve this?
I can use JPA 2.1 and tried to use @SqlResultSetMapping in combination with @ConstructorResult:
@SqlResultSetMapping(name = "AggregateStatsResult", classes = {
@ConstructorResult(targetClass = AggregateStatsDto.class,
columns = {
@ColumnResult(name = "total"),
@ColumnResult(name = "totalSum")
})
})
public class AggregateStatsDto {
private long total;
private int totalSum;
}
Query:
AggregateStatsDto result = (AggregateStatsDto) entityManager()
.createNativeQuery("select count(*) as total, select sum(field) as total_sum ... blabla", "AggregateStatsResult")
.getSingleResult();
But no luck. It seems like he wants @Entity anyway. But I just want a POJO.
org.hibernate.MappingException: Unknown SqlResultSetMapping [AggregateStatsResult]"
Thanks in advance!
source
share