English 中文(简体)
休眠条件中日期之间的限制
原标题:Restrictions Between for Date in Hibernate Criteria
  • 时间:2011-05-25 10:14:16
  •  标签:
  • hibernate

Hello I am using hibernate in my example.For bean Table Audit Trial I want to fetch audit trial between a date range with inclusion of upper & lower limits. My code is like below

Criteria criteria = session.createCriteria(AuditTrail.class);

criteria.add(Restrictions.between("auditDate", sDate, eDate));

My starting date is 25/11/2010. and end date is 25/05/2011.But it is only giving the result up to 24/05/2011.It is not performing inclusive search.Any other way to do this. I am using SQL server.

最佳回答

我认为您的审核日期实际上是一个时间戳。如果是这样的话,那么这是正常的,因为2011年5月25日意味着2011年5日凌晨0点。因此,当然,日期为2011年5月25日的每一行都是在早上0点之后。

我会在您的结束日期后添加1天,并使用auditDate>;=s日期和审核日期<;eDate

criteria.add(Restrictions.ge("auditDate", sDate)); 
criteria.add(Restrictions.lt("auditDate", eDate));
问题回答

<code>criteria.add(限制。介于(“DATE(auditDate)”,sDate,eDate)之间)使用此选项来忽略从日期开始的时间。

 criteria.add(Restrictions.ge("fromDate", DateUtil.persianToGregorian(currentDate)));
 criteria.add(Restrictions.le("toDate",  DateUtil.persianToGregorian(currentDate)));

 return criteria.list();

在结束日期后再添加一天

 @Autowired
 private SessionFactory sessionFactory;

 String startDate = "2019-07-31 ";
 String endDate = "2019-08-24 ";

 Date fromDate = format.parse(fromDate);

 /* Add one more day to the end date*/

 Date to =format.parse(endDate);
 Calendar today = Calendar.getInstance();
 today.setTime(dateto);
 today.add(Calendar.DAY_OF_YEAR, 1);

 Date toDate= format.parse(format.format(today.getTime()));

 Criteria crit = sessionFactory.getCurrentSession().createCriteria(model.class);
 crit.add(Restrictions.between("dateFieldName", fromDate, toDate));
 List result = crit.list();




相关问题
Multiple Hibernate instances using C3P0

I am facing a weird problem and it seems to be c3p0 related. I am starting two instances of an app in the same java vm which interact with each other. After some operations "APPARENT DEADLOCK" ...

Hibernate vs Ibatis caching

We can speed up a hibernate app easyly with 2nd level cache using infinispan or ehcache/terracotta,... but ibatis only have a simple interface to implement for caching. And hibernate knows more ...

Using annotations to implement a static join in hibernate

I m relatively new to hibernate and was wondering if someone could help me out. While I have no issues implementing a normal join on multiple columns in hibernate using the @JoinColumns tag, I m ...

Hibernate query with fetch question

I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements java.io.Serializable { private Integer officeId; private String addr1; private ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

Hibernate Criteria API equivalent for "elements()"

Is it possible to implement the following query using Criteria API? select order from ORDER as order,ITEM as item where item.itemID like ITM_01 and item in elements(order.items)

热门标签