在同一日期字段列中查找最小值和最大值,并使用 jpa 实体管理器 criteriabuilder 进行计数

Find min and max on the same date field column and count using jpa entity manager criteriabuilder

我正在努力替换下面的 sql 查询并使用 entitymanager criteriabuilder。我检查了其他博客和文档,但还没有成功。

SELECT MIN(creation_date),MAX(creation_date),COUNT(*), source FROM creation_tbl where creation_date>=?按来源分组;

我目前的做法

                CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Feed> criteriaQuery = criteriaBuilder.createQuery(Feed.class);
    Root<Entity> root = criteriaQuery.from(Entity.class);

    Expression es = root.<Date>get("creation_date");

    criteriaQuery.where(criteriaBuilder.greaterThanOrEqualTo(es, dateSql));

    criteriaQuery.multiselect(criteriaBuilder.greatest(root.<Date>get("creation_date")),
            criteriaBuilder.least(root.<Date>get("creation_date")),
            root.get("source"),
            criteriaBuilder.count(root)).groupBy(root.get("source"));

    Feed resultlist = entityManager.createQuery(criteriaQuery).getSingleResult();

    System.out.println(resultlist);

ETL class

@Entity
@Table(name = "creation_tbl", schema = "test")
public class ETL implements Serializable {
    private static final long serialVersionUID = 3940341617988134707L;


    @Id
    @GeneratedValue
    private long id;

    @Temporal(TemporalType.DATE)
    @Column(name = "creation_date")
    private Date creation_date;


    @Column(name = "source")
    private String source;

    public ETL() {}


    public Date getCreation_date() {
        return creation_date;
    }

    public void setCreation_date(Date creation_date) {
        this.creation_date = creation_date;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }
}

供稿class

@Entity
public class Feed implements Serializable {
    private static final long serialVersionUID = 3940341617988134707L;

    @Id
    @GeneratedValue
    private long id;


    @Temporal(TemporalType.DATE)
    @Column(name = "max")
    private Date creationDateMax;

    @Temporal(TemporalType.DATE)
    @Column(name = "min")
    private Date creationDateMin;

    @Column(name = "source")
    private String source;

    @Column(name = "count")
    private Long count;

    public Feed(long id, Date creationDateMax, Date creationDateMin, String source, Long count) {
        this.id = id;
        this.creationDateMax = creationDateMax;
        this.creationDateMin = creationDateMin;
        this.source = source;
        this.count = count;
    }

    public Feed() {}


    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public Date getCreationDateMax() {
        return creationDateMax;
    }

    public void setCreationDateMax(Date creationDateMax) {
        this.creationDateMax = creationDateMax;
    }

    public Date getCreationDateMin() {
        return creationDateMin;
    }

    public void setCreationDateMin(Date creationDateMin) {
        this.creationDateMin = creationDateMin;
    }

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }
}

错误消息 -

org.hibernate.PropertyNotFoundException: class 中没有合适的构造函数: Feed

您的查询只有 selects 4 个字段,但您的 Feed 构造函数需要 5 个字段:缺少 long id

因此,要么从构造函数中删除该参数,要么在分组时 select 额外聚合 id

P/s: Feed 看起来像一个DTO而不是实体,你不需要注释这些字段