I need to transform the results I get after running a [Select] query with Hibernate:Mutiny, but I don't know how to give the outcome a type. When I receive the list, I get a List, and when trying to map those result, I cannot access specific attributes, as they are simply intances of java.lang.Object.
Because this is just a way to organize my data, I need my 'view' to not be an Entity (saying this, as I've seen a solution that forced you to use it only on an Entity class).
sessionFactory.withSession(
session -> session.createNativeQuery(myQuery)
.getResultList().map(MyListView::ProcessData);
Where I receive this List:
public class MyListView{
private List<MyIndividualView> items;
public static MyListView processResults(List<Object> results){
// Transform data.
}
}
The List seems to contain Object instances, trying to cast to the classes this kind of functionality usually returns (Like JsonObject, Map or Row) throws a ClassCastException.
I found another StackOverflow entry similar (will add the link below), but they solved it by 'unwrapping' the query to then a 'transformer' to map it:
query.unwrap(SQLQuery.class)
.addScalar("total", LongType.INSTANCE)
.addScalar("amountOfSales", LongType.INSTANCE)
.addScalar("amountOfProducts", LongType.INSTANCE)
.setResultTransformer(Transformers.aliasToBean(SaleStatsInfo.class));
I don't have access to that unwrap method. Maybe that's a different dependency or something, I don't really know, it's just not an option as I'm using it right now. Any idea how to solve this ? Thank you for reading.
The link I was referring to :createNativeQuery mapping to POJO (non-entity)
Solution: It seems what I was getting is, actually, a list of arrays (Object[]), and you can access all attributes by knowing the order and cast them to the right type. It's not the fanciest way to handle something like this, but, by lack of any other solution, will have to do.