Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate从数据库获取对象,其中日期等于给定日期_Hibernate_Date_Hql - Fatal编程技术网

Hibernate从数据库获取对象,其中日期等于给定日期

Hibernate从数据库获取对象,其中日期等于给定日期,hibernate,date,hql,Hibernate,Date,Hql,我想从数据库中获取给定日期时间范围内的对象列表。我如何做到这一点?这是我目前使用的解决方案,但它不会返回任何内容。。。并且给定的值是正确的 public List getRegistrationsForUserOnDate(Date startdate, Date enddate){ Set<Date> dateSet = new HashSet<Date>(); Calendar calendar = new GregorianCalen

我想从数据库中获取给定日期时间范围内的对象列表。我如何做到这一点?这是我目前使用的解决方案,但它不会返回任何内容。。。并且给定的值是正确的

public List getRegistrationsForUserOnDate(Date startdate, Date enddate){
        Set<Date> dateSet = new HashSet<Date>();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(enddate);

        while (calendar.getTime().before(startdate))
        {
            Date resultado = calendar.getTime();
            dateSet.add(resultado);
            calendar.add(Calendar.DATE, 1);
        }

        String query = "from Registration where date = '" + Validation.convertToValidDateTime(startdate, true) + "'";
        for(Date date : dateSet){
            query += " AND date = '" + Validation.convertToValidDateTime(date, true)+"'";
        }             
        return em.createQuery(query).getResultList();           
    }
然后此方法查询如下所示(从调试模式复制的数据):

以下是数据库中存在的一些示例字段:

id |       date          |comment|FK|FK|FK
132|"2012-06-08 00:00:00"|    "" | 6| 2| 1
133|"2012-06-15 00:00:00"|    "" | 8| 1| 1
134|"2012-06-14 00:00:00"|    "" | 3| 2| 1
135|"2012-06-13 00:00:00"|    "" | 1| 3| 1

日期怎么可能等于两个不同的日期?你什么都没得到并不奇怪

日期='2012-06-15 00:00:00'和日期='2012-06-14 00:00:00'-如果今天的日期是星期四和星期五,还吗

你应该使用或而不是和,或者更好的是,大于或小于或介于两者之间

from TimeRegistration where date between '2012-06-13 00:00:00' and '2012-06-15 00:00:00'
HQL: 结果: 返回其日期属性等于date1、date2或date3的所有对象。 我现在使用我创建的以下方法,只需声明一个开始日期和一个结束日期,它就可以获得这两个日期之间的所有对象,它就像一个符咒


啊。。是的,愚蠢的我。。我试着使用OR子句,但也不起作用。。还有其他建议吗?确切的日期匹配可能会有问题。除非有迫切要求您的日期正好在这一秒内,否则我会选择介于或日期>='2012-06-13 00:00:00'和我使用此查询ATM的日期,并在数据库上运行:从tbl_注册中选择*,其中日期在('2012-06-15 00:00:00','2012-06-14 00:00:00','2012-06-13 00:00:00')但是当我在我的代码中实现这一点时,它突然不再起作用了这是从我的方法中发送的查询:从注册的where date in('2012-06-15 00:00:00','2012-06-14 00:00:00','2012-06-13 00:00:00'),并且您确定您的日期格式与预期的一样,并且“date”是您正在读取的对象中的属性吗?你能看一下数据库的日志,看看到底发生了什么吗?
id |       date          |comment|FK|FK|FK
132|"2012-06-08 00:00:00"|    "" | 6| 2| 1
133|"2012-06-15 00:00:00"|    "" | 8| 1| 1
134|"2012-06-14 00:00:00"|    "" | 3| 2| 1
135|"2012-06-13 00:00:00"|    "" | 1| 3| 1
from TimeRegistration where date between '2012-06-13 00:00:00' and '2012-06-15 00:00:00'
from objectname where date in ('date1','date2','date3')
public List getRegistrationsForUserOnDate(Date startdate, Date enddate)throws Exception{
        Collection<Date> dateList = new ArrayList<Date>();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(enddate);

        while (calendar.getTime().before(startdate)){
            Date resultado = calendar.getTime();
            dateList.add(resultado);
            calendar.add(Calendar.DATE, 1);
        }

            String query = "from Registration t where t.date in ('" + Validation.convertToValidDateTime(startdate, true) + "'";
            for(Date date : dateList){
                query += ", '" + Validation.convertToValidDateTime(date, true)+"'";
            }
            query += ")";

            return em.createQuery(query).getResultList();
        }
public static String convertToValidDateTime(Date date, boolean clearTime)throws Exception{
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateFormatRemoveTime = new SimpleDateFormat("yyyy-MM-dd");
        return clearTime
                ?dateFormat.format(dateFormatRemoveTime.parse(dateFormatRemoveTime.format(date)))
                :dateFormatRemoveTime.format(date);
    }