JPA + Hibernate currentTimestamp() 与预期类型不匹配
JPA + Hibernate currentTimestamp() did not match expected type
我有以下代码抛出 java.lang.IllegalArgumentException: Parameter value [org.hibernate.query.criteria.internal.expression.function.CurrentTimestampFunction@30cb223b] did not match expected type [java.util.Date (n/a)]
@Transactional
open fun delete(entity: E) {
val cb = em.criteriaBuilder
// create update query
val query = cb.createCriteriaUpdate(Entity::class.java)
val updateEntity = query.from(Entity::class.java)
// set update and where clause
query.set("deletedOn", cb.currentTimestamp()) // <- problem exhibited due to this line
query.where(cb.equal(updateEntity.get<Long>("id"), entity.id))
// perform update
em.createQuery(query).executeUpdate()
}
错误是由于我使用 CriteriaBuilder 的 currentTimestamp()
。在 validate(Type paramType, Object bind, TemporalType temporalType)
中的 Hibernate QueryParameterBindingValidator
中有这一行
final Class parameterType = paramType.getReturnedClass();
实体的 deletedOn
类型为 Timestamp
。 paramType
指的是该字段并且是 TimestampType
类型(来自 Hibernate)。当 getReturnedClass()
被调用时,它是 returns java.util.Date
(我猜是因为这是基本类型?)。不幸的是,表达式 cb.currentTimestamp()
发生类型不匹配并抛出错误。
我无法在网上找到有关如何使用 CriteriaBuilder 中的 currentTimestamp()
来完成此任务的工作示例。
如有任何帮助,我们将不胜感激!谢谢!
我发现使用这条线就可以了。
query.set(updateEntity.get<Timestamp>("deletedOn"), cb.currentTimestamp())
我有以下代码抛出 java.lang.IllegalArgumentException: Parameter value [org.hibernate.query.criteria.internal.expression.function.CurrentTimestampFunction@30cb223b] did not match expected type [java.util.Date (n/a)]
@Transactional
open fun delete(entity: E) {
val cb = em.criteriaBuilder
// create update query
val query = cb.createCriteriaUpdate(Entity::class.java)
val updateEntity = query.from(Entity::class.java)
// set update and where clause
query.set("deletedOn", cb.currentTimestamp()) // <- problem exhibited due to this line
query.where(cb.equal(updateEntity.get<Long>("id"), entity.id))
// perform update
em.createQuery(query).executeUpdate()
}
错误是由于我使用 CriteriaBuilder 的 currentTimestamp()
。在 validate(Type paramType, Object bind, TemporalType temporalType)
中的 Hibernate QueryParameterBindingValidator
中有这一行
final Class parameterType = paramType.getReturnedClass();
实体的 deletedOn
类型为 Timestamp
。 paramType
指的是该字段并且是 TimestampType
类型(来自 Hibernate)。当 getReturnedClass()
被调用时,它是 returns java.util.Date
(我猜是因为这是基本类型?)。不幸的是,表达式 cb.currentTimestamp()
发生类型不匹配并抛出错误。
我无法在网上找到有关如何使用 CriteriaBuilder 中的 currentTimestamp()
来完成此任务的工作示例。
如有任何帮助,我们将不胜感激!谢谢!
我发现使用这条线就可以了。
query.set(updateEntity.get<Timestamp>("deletedOn"), cb.currentTimestamp())