【问题标题】:JPA + Hibernate currentTimestamp() did not match expected typeJPA + Hibernate currentTimestamp() 与预期类型不匹配
【发布时间】:2019-04-30 21:35:25
【问题描述】:

我有以下代码抛出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() 造成的。在 Hibernate 的 QueryParameterBindingValidator 中的 validate(Type paramType, Object bind, TemporalType temporalType) 中有这一行

final Class parameterType = paramType.getReturnedClass();

实体的deletedOnTimestamp 类型。 paramType 指的是该字段并且是TimestampType 类型(来自Hibernate)。当getReturnedClass() 被调用时,它返回java.util.Date(我猜是因为那是基本类型?)。不幸的是,表达式cb.currentTimestamp() 出现类型不匹配并引发错误。

我在网上找不到关于如何使用 CriteriaBuilder 中的currentTimestamp() 来完成此任务的工作示例。

任何帮助将不胜感激!谢谢!

【问题讨论】:

    标签: hibernate jpa kotlin criteria


    【解决方案1】:

    我发现使用这条线就可以了。

    query.set(updateEntity.get<Timestamp>("deletedOn"), cb.currentTimestamp())
    

    【讨论】:

    • 正确的菱形语法是updateEntity.&lt;Timestamp&gt;get("deletedOn")
    猜你喜欢
    • 2016-07-13
    • 2015-08-17
    • 1970-01-01
    • 2013-02-20
    • 2019-02-11
    • 2014-06-09
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多