我在postgres中有一张表
CREATE TABLE employer_visit(
employer_id INTEGER REFERENCES employer,
visit_counter INTEGER NOT NULL, -- counter on current date
visit_before_date_total_counter INTEGER NOT NULL, -- total, since service has started
date DATE,
PRIMARY KEY(employer_id, date)
);
我需要通过过去30天的访问找到最佳雇主 - (最后一条记录中visit_before_date_total_counter与30天前最后一条记录之间的差异employer_visit
的有序列表)
我试过这个hql查询
Query<EmployerVisit> query = getSession().createQuery(
"SELECT ev FROM EmployerVisit ev " +
"WHERE ev.employerVisitId.date = (SELECT MAX (groupedEv.employerVisitId.date) FROM EmployerVisit groupedEv " +
"WHERE groupedEv.employerVisitId.employerId = ev.employerVisitId.employerId) " +
"AND " +
"ev.employerVisitId.date = (SELECT MAX (groupedEv2.employerVisitId.date) < :date FROM EmployerVisit groupedEv2 " +
"WHERE groupedEv2.employerVisitId.employerId = ev.employerVisitId.employerId) " +
"ORDER BY (groupedEv.visitCounter + groupedEv.visitBeforeDateTotalCounter) - (groupedEv2.visitCounter + groupedEv2.visitBeforeDateTotalCounter) DESC"
).setMaxResults(size).setParameter("date", calendar.getTime());
但它落在了
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'groupedEv.visitCounter'
在groupedEv
ORDER BY
表格不存在
在hibernate中可以对这种情况使用depricated SqlQuery但是我更喜欢hql,请帮助,谢谢。
UPDATE! - 没有setter和getter的Entity和EmbeddedId类
@Entity
@Table(name = "employer_visit")
public class EmployerVisit {
@EmbeddedId
private EmployerVisitId employerVisitId;
@Column(name = "visit_counter")
private Integer visitCounter;
@Column(name = "visit_before_date_total_counter")
private Integer visitBeforeDateTotalCounter;
public EmployerVisit() {
employerVisitId = new EmployerVisitId();
visitCounter = 0;
visitBeforeDateTotalCounter = 0;
}
}
@Embeddable
public class EmployerVisitId implements Serializable {
@Column(name = "employer_id")
private Integer employerId;
@Column(name = "date")
private Date date;
public EmployerVisitId() {
}
}