Java, Spring Data JPA
I have 2 entities:
class Source {
Integer id;
String name;
}
class Item {
Integer id;
String name;
Integer sourceId;
}
I need statistic native query result like this:
select s.id source_id, s.name source_name, count(i.id) item_count
from source s
left join item i on s.id = i.source_id
group by s.id
And i want to have result in Java object MyResult:
class MyResult {
Source source;
Integer itemCount;
MyResult(Source source, Integer itemCount) {...}
}
The closest solution is using @SqlResultSetMapping like this:
@SqlResultSetMapping(
name = "MyResultMapping",
entities = {
@EntityResult(
entityClass = Source.class,
fields = {
@FieldResult(name = "id", column = "source_id"),
@FieldResult(name = "name", column = "source_name"),
}
),
...
???
}
)
OR
@SqlResultSetMapping(
name = "MyResultMapping",
classes = {
@ConstructorResult(
targetClass = MyResult.class,
columns = {
@ColumnResult(name = "???"),
???
}
)
}
)
With second variant i can use something like this:
MyResult(Integer sourceId, String sourceName, Integer itemsCount) {
this.source = new Source(sourceId, sourceName);
this.itemsCount = itemsCount;
}
but i want it to automate with @SqlResultSetMapping... (because my real objects more complex)