休眠为每个组选择第一行

时间:2018-08-02 17:56:54

标签: java hibernate jpa group-by hql

您好,我们可以通过HQL做到这一点吗? 在这里我返回DTO列表而不是Object Class列表。 我的SQL查询是

select * from readings   join  
        (select max(signal) as signal,sender as sr ,receiver as rc from readings  group by readings.sender,readings.receiver)a 
    on  
    a.signal=readings.signal and 
    a.sr=readings.sender and 
    a.rc=readings.receiver

这是我的DTO / Bean / Pojo类

public class Readings implements java.io.Serializable {

    private Integer id;
    private String sender;
    private String major;
    private String minor;
    private int signal;
    private BigDecimal power;
    private BigDecimal temperature;
    private String battery;
    private String receiver;
    private Date createdDatetime;..

and getters and setters ...

HQL中的哪个是

Criteria cr = session.createCriteria(com.XYZ..Readings.class)
                        .setProjection(Projections.projectionList()
                        .add(Projections.max("signal"))
                       .add(Projections.groupProperty("sender"))
                        .add(Projections.groupProperty("receiver")));


List<Readings>  br=(List<Readings>)cr.list();

这在最后一行失败,尝试将Object强制转换为Readings类。

0 个答案:

没有答案