2011-03-12 27 views
2

我想查找在特定日期处于活动状态的所有事件实体。例如:查找Hibernate实体,其日期介于可以为null的开始日期和结束日期之间

from Event where startDate <= :specifiedDate and endDate > :specifiedDate 

然而,Event.startDateEvent.endDate可以null。 A null值表示没有开始或结束日期(过去的开始是无限的,或者将来的结束是无限的)。

因此,if Event.startDate = null and Event.endDate = 2011-05-01,那么对于小于2011-05-01的所有指定日期值,应该返回该事件。

if Event.startDate = 2011-03-01 and Event.endDate = null,那么该事件应返回所有指定的日期值大于或等于2011-03-01。

最后,if Event.startDate = null and Event.endDate = null,那么应该返回任何指定日期的事件。

这可能使用Hibernate和MySQL后端?

回答

4

这当然是可能的。

使用的标准应该是这样的:

List events = sess.createCriteria(Event.class) 
       .add(Restrictions.or(
          Restrictions.isNull("startDate"), 
          Restrictions.ge("startDate", specifiedDate)) 
       .add(Restrictions.or(
          Restrictions.isNull("endDate"), 
          Restrictions.lt("endDate", specifiedDate)) 
       .list(); 
+0

感谢,看起来像它的工作! – Tauren

相关问题