[英]Criteria Restriction by date field
I have 2 tables, Partner
and Visit
.我有 2 张桌子,
Partner
和Visit
。 A partner can have many visits, also various in same day.一个合作伙伴可以有很多次访问,也可以在同一天进行多次访问。 I need to create a
Criteria
restriction to know how many days the Partner
has visits since a given date.我需要创建一个
Criteria
限制以了解Partner
自给定日期以来访问了多少天。 So, 2 or more visits in same date must be only one.因此,同一日期的 2 次或多次访问必须只有 1 次。
Can this be done only by Criteria
and Restrictions
??这只能通过
Criteria
和Restrictions
来完成吗?
I can get all visits from a date with a criteria like:我可以从具有以下条件的日期获得所有访问:
Criteria criteria = buildCriteria();
criteria.add(Restrictions.eq(DBConstants.VISIT_COL_VISITOR, partnerVisitor));
criteria.add(Restrictions.ge(DBConstants.VISIT_COL_DATE, startDate));
But now, to filter repeated days, I need something like:但是现在,要过滤重复的日子,我需要类似的东西:
criteria.add(Restrictions.unique(DBConstants.VISIT_COL_DATE));
Any idea?任何想法?
EDIT: @user23123412 @netik编辑: @user23123412 @netik
Visit.java
private Integer id;
private Date date;
private Partner visitor;
// getters + setters
Visit
table rows related to partner 1: Visit
与合作伙伴 1 相关的表行:
ID ![]() |
VISITOR![]() |
DATE![]() |
---|---|---|
1 ![]() |
1 ![]() |
10/10/2014 16:20 ![]() |
20 ![]() |
1 ![]() |
10/10/2014 18:00 ![]() |
45 ![]() |
1 ![]() |
12/10/2014 16:20 ![]() |
71 ![]() |
1 ![]() |
12/10/2014 19:40 ![]() |
89 ![]() |
1 ![]() |
16/10/2014 11:20 ![]() |
The answer I need after the query is a Visit
count in different days since a given date
.查询后我需要的答案是自给定
date
以来不同天数的Visit
计数。
IE: If i launch a query with visitor = 1
and startDate = 12/10/2014
the result MUST be 2, cause row id = 45
and id = 71
have different visits in a same day, so it's only ONE day. IE:如果我使用
visitor = 1
和startDate = 12/10/2014
启动查询,结果必须为 2,因为行id = 45
和id = 71
在同一天有不同的访问,所以只有一天。
You have three options:你有三个选择:
1) If you are not pinned to Critera api, I recommend to use HQL instead of Criteria API 1)如果您没有固定到 Critera api,我建议使用 HQL 而不是 Criteria API
Date d = new SimpleDateFormat("yyyy-MM-dd").parse("2014-10-12");
Query query = session.createQuery("select count(*) from Visit v where trunc(v.date)=:date and v.visitor.id=:visitorId");
query.setParameter("date", d);
query.setParameter("visitorId", 1L);
Long count = (Long) query.uniqueResult();
2) If you want to use Criteria API, it's possible to apply sqlRestriction. 2) 如果要使用 Criteria API,可以应用 sqlRestriction。 Unfortunately you will lock to specific database.
不幸的是,您将锁定到特定的数据库。 This example works on HSQLDB
此示例适用于 HSQLDB
Date d = new SimpleDateFormat("yyyy-MM-dd").parse("2014-10-12");
Long count = (Long) session.createCriteria(Visit.class)
.setProjection(Projections.rowCount())
.add(Restrictions.eq("visitor.id", 1L))
.add(Restrictions.sqlRestriction("trunc(date)=?", d, org.hibernate.type.StandardBasicTypes.DATE))
.uniqueResult() ;
3) It's also possible to use pure criteria API, but date restriction must be a little bit hacked (using between restriction) 3) 也可以使用纯标准 API,但日期限制必须有点黑客(使用限制之间)
Date d = new SimpleDateFormat("yyyy-MM-dd").parse("2014-10-12");
Date maxDate = new Date(d.getTime() + TimeUnit.DAYS.toMillis(1));
Long count = (Long) session.createCriteria(Visit.class)
.setProjection(Projections.rowCount())
.add(Restrictions.eq("visitor.id", 1L))
.add(Restrictions.between("date", d, maxDate))
.uniqueResult();
Well... finally, after almost getting crazy, I've found solution to my query using @netik first solution with some modifications: I've found out problem was mysql
was not supporting trunc
.好吧...最后,在几乎发疯之后,我找到了使用@netik 第一个解决方案并进行了一些修改的查询解决方案:我发现问题是
mysql
不支持trunc
。 No matter hql
does.不管
hql
做什么。
So finally the query method after data processing is like that:所以最后数据处理后的查询方法是这样的:
public Integer getPartnerVisitDatesSinceDate(Date startDate, Long partnerId) {
Query query = getSession().createQuery("select count(distinct visit.date) from Visit visit where date(visit.date) >= :date and visit.visitor.id = :visitorId");
query.setDate("date", startDate);
query.setParameter("visitorId", partnerId);
return (Integer) query.uniqueResult();
}
Thanks @netik!谢谢@netik!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.