I have tree of classes:
classA {
classB b;
classC c;
.....
}
I have HQL query like this:
SELECT a.field1, b.field2, c.field3, c.field4
FROM a LEFT OUTER JOIN b ON a.id = b.fk
LEFT OUTER JOIN c ON b.id = c.fk
This query returns List<Object[]>
.
Is it possible to cast the returned data to the following class:
classD {
Type1 fiedl1;
Type2 field2;
Type3 field3;
}
So can casting be made by Hibernate or I need manually do all casting?
There are different types of selects in JPA queries. You are currently using Array as a return type, what you need is Construct return type. Here is how to achieve this:
String queryStr =
"select NEW package.YourDefinedCustomClass(
a.field1, b.field2, c.field3, c.field4) from a left outer join b
on a.id=b.fk left outer join c on b.id=c.fk";
TypedQuery<YourDefinedCustomClass> query =
em.createQuery(queryStr, YourDefinedCustomClass.class);
List<YourDefinedCustomClass> results = query.getResultList();
Basically there are two things:
Read more on selects in JPA2 queries.
If you are really sure you can do type casts.
List<classD> newlist = ...;
for(Object o : list){
newlist.add((classD) o);
}
Be careful with this though
So yes. Manual casting. (note: with arrays (you can directly cast) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With