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

时间:2014-07-06 16:39:36

标签: java hibernate orm hql hibernate-mapping

我正在尝试执行以下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??
         }

1 个答案:

答案 0 :(得分:1)

这是您获取结果的方法:

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