I have tree of classes:
classA {
classB b;
classC c;
.....
}
I have HQL query like this:
You can use TypedQuery
TypedQuery<ClassA> q = em.createQuery("select a from a left outer join b on a.id=b.fk left outer join c on b.id=c.fk", ClassA.class);
List<ClassA> res = q.getResultList();
i had same problem..i wrote this query
Query sqlquery = session.createQuery("select c.courseName,f.facultyID,f.facultyName,f.facultyEmailID,f.facultyContactNo,s.subjectName from com.bean.CourseBean as c,com.bean.FacultyBean as f,com.bean.Faculty_SubjectBean as fs,com.bean.SubjectBean as s where f.facultyID=fs.facultyBean.facultyID AND s.subjectID=fs.subjectBean.subjectID AND c.courseID=f.courseBean.courseID AND collegeid=1");
and i returned list of object and in servlet i wrote,
java.util.List objList= objFacultyService.listFaculty_sql(1);
java.util.List<Temp> objtemp = new ArrayList<Temp>() ;
for (Object[] objects : objList)
{
Temp temp = new Temp();
temp.setFacultyEmailID(objects[3].toString());
temp.setCourseName(objects[0].toString());
if(objects[4]==null)
{
temp.setFacultyContactNo(1);
}
else
{
temp.setFacultyContactNo(Long.parseLong(objects[4].toString()));
}
temp.setFacultyID(Long.parseLong(objects[1].toString()));
temp.setFacultyName(objects[2].toString());
temp.setSubjectName(objects[5].toString());
objtemp.add(temp);
}
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 use Hibernate 5.x, you can use a ResultTransformer
, as, e.g., described in this blog post. Note that ResultTransformer
will be @Deprecated in Hibernate 6.
Applied to your scenario:
List<classD> classDDTOs = entityManager
.createQuery(
"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")
.unwrap( org.hibernate.query.Query.class )
.setResultTransformer(
new ResultTransformer() {
@Override
public Object transformTuple(
Object[] tuple,
String[] aliases) {
return new classD(
(Type1) tuple[0],
(Type2) tuple[1],
(Type3) tuple[1],
);
}
@Override
public List transformList(List collection) {
return collection;
}
}
)
.getResultList();
The above code assumes classD
has a constructor classD(Type1, Type2, Type3)
.
An advantage of this approach is that it lets you define the ResultTransformer
once and allows you to reuse it in all queries with the same result type.