Issue
I am trying to perform following HQL query :
select count(*) , od.itemSubtype.name from OrderDetails od inner join od.itemSubtype where od.employee = 1 group by od.itemSubtype
My POJOs are :
OrderDetails
-------------
private Integer id;
private Employee employee;
private Order order;
private ItemSubtype itemSubtype;
private Integer quantity;
private String imageStyle;
private String imageCapture;
private Date workStartedOn;
private Date workFinishedOn;
ItemSubtype
-----------
private Integer id;
private ItemType itemType;
private String name;
private Set orderDetailses = new HashSet(0);
private Set measuresBooks = new HashSet(0);
As the query is not able to cast existing Pojo, I am unable to retrive result. How can I perform this query? Do I need to write a new Pojo? Also How do i get Data from iteration ?
Query query = session.createQuery(hql);
// query.setParameter("employee_id", employee_id);
Iterator i = query.iterate();
while (i.hasNext()) {
Object obj = i.next();
// how to retrive values from this obj??
}
Solution
This is how you can obtain the results:
Query query = session.createQuery("""
select
count(ist.id),
ist.name
from OrderDetails od
inner join od.itemSubtype ist
where od.employee = 1
group by ist.name
"""
);
for(Object rows : query.list()){
Object[] row = (Object[]) rows;
Number count = (Number) row[0];
String itemSubTypeName = (String) row[1];
}
Answered By - Vlad Mihalcea
Answer Checked By - Mary Flores (JavaFixing Volunteer)