问题
I am working on replacing below sql query and use entitymanager criteriabuilder. I checked other blogs and documentation but haven't been successful yet.
SELECT MIN(creation_date),MAX(creation_date),COUNT(*), source FROM creation_tbl where creation_date>=? GROUP BY source;
My current approach
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;
}
}
Feed 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;
}
}
Error message -
org.hibernate.PropertyNotFoundException: no appropriate constructor in class: Feed
回答1:
Your query only selects 4 fields but your Feed
constructor requires 5 fields: long id
is missing.
So either remove that parameter from constructor or select an additional aggregated id
when grouping.
P/s: Feed
looks like a DTO instead of entity, you do not need to annotate these fields
来源:https://stackoverflow.com/questions/60441796/find-min-and-max-on-the-same-date-field-column-and-count-using-jpa-entity-manage