如何比较hql中的时间戳字段?

cotxawn7  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(278)

我在表中有一个java.util.timestamp列´我无法与where子句中的变量进行比较。使用此代码:

// Using Calendar to convert Java Date 'horarioEliminar' to Sql Timestamp 't'
        Calendar cal = Calendar.getInstance();
        cal.setTime(horarioEliminar);
        cal.set(Calendar.MILLISECOND, 0);

        Timestamp t = new java.sql.Timestamp(cal.getTimeInMillis());

        String horQ = "FROM Academia.Horarios WHERE horario ="+ t;
        horario = (Horarios) session.createQuery(horQ).uniqueResult();

我有个例外:
错误[http-nio-8080-exec-873]org.hibernate.hql.internal.ast.errorcounter.reporterror行1:57:意外标记:10
但我检查了system.out.println(t)结果是否与horario列中存储的值匹配。如何正确比较这两个时间戳?

mkh04yzy

mkh04yzy1#

尝试将时间戳设置为如下查询参数:

Timestamp t = new java.sql.Timestamp(cal.getTimeInMillis());

 String horQ = "FROM Academia.Horarios WHERE horario = :ts";
 horario = (Horarios) session.createQuery(horQ).setParameter("ts", t).uniqueResult();

相关问题