如何在休眠期间检查

时间:2017-04-13 09:03:52

标签: hibernate

此查询在所有情况下都能正常工作,期望一个条件例如 StartDate 2017-04-17 09:00:00和endDate 2017-04-17 10:00:00它将采取它是正确的,如果v输入StartDate 2017-04-17 09:05:00和endDate 2017-04- 17 09:50:00它不会允许它正确但如果我输入StartDate 2017-04-17 10:30:00和endDate 2017-04-17 011:30:00之间的时间应该只允许时间9到10存在而不是10.30到9.30所以如何允许这个 输入

 Query query = session.createQuery("from TableDTO where cName =? and ((date(startDate) between date(:std) AND date(:etd))AND (HOUR (startDate)  BETWEEN :stDate AND :edDate))or((date(endDate) between date(:std) AND date(:etd))AND( HOUR (endDate)  BETWEEN :stDate AND :edDate))");
        query.setParameter(0, timeTableDTO.getClassName()); 
        query.setParameter("std", timeTableDTO.getStartDate()); 
        query.setParameter("etd", timeTableDTO.getEndDate());   

        query.setParameter("stDate",  starthr);
        query.setParameter("edDate", endthr);

2 个答案:

答案 0 :(得分:1)

您可以写 HQL

ResultTransformer将生成的查询结果转换为模型和 set参数是查询参数的占位符。

Query query =  ((SQLQuery) getSession().createSQLQuery(
                "select First_name,Last_name,email_id from tabletbl  where (date(StartDate) between date('2017-04-17 08:05:00') AND
    date('2017-04-17 08:50:00')
    AND HOUR (StartDate) BETWEEN 8 AND 9) or(date(EndDate) between
    date('2017-04-17 08:05:00') AND
    date('2017-04-17 08:50:00')
    AND HOUR (EndDate) BETWEEN 8 AND 9) ).
                setResultTransformer(Transformers.aliasToBean(tabletbl.class))).                
                setParameter("StartDate", StartDate);    
List<tabletblModelClass> list = query.list();

答案 1 :(得分:0)

您可以使用下面的条件,使用大于等于或小于等于gele的组合:

// assuming startdate and endDate are Date objects with the values in them as you wish. and your Criteria is called criteria
criteria.add(Restrictions.ge("startDate", startDate)); 
criteria.add(Restrictions.lt("startDate", endDate));

或者,您也可以使用Restrictions.between

criteria.add(Restrictions.between("startDate",startDate,endDate);

文档here ...

  

静态标准
  之间(String propertyName,Object lo,Object hi)

     

将“between”约束应用于命名属性