gpt4 book ai didi

java - Hibernate uniqueResult 为空,但它不应该

转载 作者:行者123 更新时间:2023-11-30 10:52:47 26 4
gpt4 key购买 nike

早上好

我正在尝试以下操作:

在更新对象之前,我想从最后一条记录中检索一个字段以将其添加到新行中。所以这就是我所做的

 int idLuce = Integer.parseInt(msg);
Illuminazione illuminazione = new Illuminazione();
session.beginTransaction();
selectQuery = "SELECT dateTime from Illuminazione i where i.idLuce = :idLuce ORDER BY i.dateTime ASC";
theQuery = session.createQuery(selectQuery).setProperties(illuminazione).setFirstResult(0)
.setMaxResults(1);
lastDate = (Date) theQuery.uniqueResult();
logger.debug("last date time is: "+lastDate);
illuminazione.setLastDateTime(lastDate);
illuminazione.setIdLuce(idLuce);
illuminazione.setIsLit(true);
Date date = new Date();
illuminazione.setDateTime(new Timestamp(date.getTime()));
session.save(illuminazione);
session.getTransaction().commit();

一切如我所料,但 lastDate 为空...我没有收到任何错误或异常,这是操作日志..

2015-12-15 10:30:01.157 DEBUG [Thread-2]: HQL: SELECT dateTime from it.besmart.models.Illuminazione i where i.idLuce = :idLuce ORDER BY i.dateTime ASC
2015-12-15 10:30:01.159 DEBUG [Thread-2]: SQL: select illuminazi0_.date_time as col_0_0_ from SMARTPARK.illuminazione illuminazi0_ where illuminazi0_.id_luce=? order by illuminazi0_.date_time ASC
2015-12-15 10:30:01.161 DEBUG [Thread-2]: throwQueryException() : no errors

Hibernate:
select
illuminazi0_.date_time as col_0_0_
from
SMARTPARK.illuminazione illuminazi0_
where
illuminazi0_.id_luce=?
order by
illuminazi0_.date_time ASC limit ?
2015-12-15 10:30:01.187 DEBUG [Thread-2]: last date time is: null

2015-12-15 10:30:01.285 DEBUG [Thread-2]: Executing identity-insert immediately
2015-12-15 10:30:01.314 DEBUG [Thread-2]:
insert
into
SMARTPARK.illuminazione
(date_time, id_luce, isLit, last_date_time)
values
(?, ?, ?, ?)
Hibernate:
insert
into
SMARTPARK.illuminazione
(date_time, id_luce, isLit, last_date_time)
values
(?, ?, ?, ?)

显然,如果我在 SQL 中运行相同的查询,它会起作用...

我做错了什么?谢谢

最佳答案

您似乎没有将 idLuce 传递到查询中。试试这个

int idLuce = Integer.parseInt(msg);
session.beginTransaction();
selectQuery = "SELECT dateTime from Illuminazione i where i.idLuce = :idLuce ORDER BY i.dateTime ASC";
theQuery = session.createQuery(selectQuery).setParameter("idLuce", idLuce).setMaxResults(1);
lastDate = (Date) theQuery.uniqueResult();

或者,在执行查询之前移动 illuminazione.setIdLuce(idLuce); 行。

关于java - Hibernate uniqueResult 为空,但它不应该,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34285516/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com