HQL Hibernate INNER JOIN子查询

时间:2018-07-26 15:00:53

标签: java hibernate hql hibernate-criteria

我有一些SQL方面的经验,但是我对HQL的了解有限

这是我的SQL查询

#include

注意:最外面的查询是仅获取明确的结果。

还有DTO

select * from 
    (select * from readings   join  
        (select max(signal) as signal,sender as mac ,receiver as rc from readings  where created_datetime  between '2018-02-05 10:25:52' AND '2018-04-05 10:25:52' group by readings.sender,readings.receiver)a 
    on  
    a.signal=readings.signal and 
    a.mac=readings.sender and 
    a.rc=readings.receiver ) c  
group by c.sender,c.receiver;

到目前为止,我只能通过上述两种方法来完成最内层的查询,但是我有点被困在那里。

此作品有效>> public class Readings implements java.io.Serializable { private Integer id; private String sender; private String major; private String minor; private int signal; private BigDecimal temperature; private String batteryLife; private String receiver; private Date createdDatetime; } Criteria cr = session.createCriteria(com.xyz.hibernate.dto.Readings.class) .setProjection(Projections.projectionList() .add(Projections.max("signal")) .add(Projections.groupProperty("sender")) .add(Projections.groupProperty("receiver"))); cr.add(Restrictions.between("createdDatetime", from, to)); String query="select max(r.signal) as signal,r.sender as mac ,r.receiver as rc from com.xyz.hibernate.dto.Readings r where r.createdDatetime between :from AND :to group by r.sender,r.receiver"; Query createQuery = session.createQuery(query); createQuery.setParameter("from", from); createQuery.setParameter("to", to);

但是这不是>> select max(r.signal) as signal,r.sender as mac ,r.receiver as rc from com.xyz.hibernate.dto.Readings r where r.createdDatetime between :from AND :to group by r.sender,r.receiver

如果可能的话,我想采用第一种方法,例如纯HQL,但我真的怀疑那是否可能。

任何建议都会很有帮助

0 个答案:

没有答案