简体   繁体   English

按日期字段的条件限制

[英]Criteria Restriction by date field

I have 2 tables, Partner and Visit .我有 2 张桌子, PartnerVisit 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 ??这只能通过CriteriaRestrictions来完成吗?

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 ID VISITOR游客 DATE日期
1 1 1 1 10/10/2014 16:20 2014 年 10 月 10 日 16:20
20 20 1 1 10/10/2014 18:00 2014 年 10 月 10 日 18:00
45 45 1 1 12/10/2014 16:20 2014 年 12 月 10 日 16:20
71 71 1 1 12/10/2014 19:40 2014 年 12 月 10 日 19:40
89 89 1 1 16/10/2014 11:20 2014 年 16 月 10 日 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 = 1startDate = 12/10/2014启动查询,结果必须为 2,因为行id = 45id = 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM