hibernate POJO不相关或不可用时的HQL查询

uyto3xhc  于 2022-11-14  发布在  其他
关注(0)|答案(1)|浏览(115)

我正在尝试执行以下HQL查询:

select count(*) , od.itemSubtype.name from OrderDetails od inner join od.itemSubtype where od.employee = 1 group by od.itemSubtype

我的POJO是:

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);

由于查询无法强制转换现有的POJO,因此我无法检索结果。如何执行此查询?我需要写一个新的POJO吗?另外,我如何从迭代中获得数据?

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??
         }
dsekswqp

dsekswqp1#

以下是获取结果的方法:

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];
}

相关问题