【问题标题】:Prepared statement in Hibernate doesn't workHibernate 中的准备好的语句不起作用
【发布时间】:2012-02-16 15:27:25
【问题描述】:

当我执行以下查询时:

return (Formation) em
    .createQuery(
            "SELECT f FROM Formation f WHERE f.model" + modelType + "= :modelId")
    .setParameter("modelId", modelId)
    .getSingleResult();

我收到此错误:

org.hibernate.PropertyAccessException: 无法通过 model.configuration.Model.id 的反射 getter 获取字段值

当我用上面那个丑陋的查询替换准备好的语句时,它可以工作:

return (Formation) em
    .createQuery(
            "SELECT f FROM Formation f WHERE f.model" + modelType + "= " + modelId)
    .getSingleResult();

【问题讨论】:

  • Formation 类中的modelId 属性有getter 和setter 吗?
  • 我不明白为什么我需要一个名为 modelId 的 getter 和一个 setter,而我没有它们,因为我没有任何名为 modelId 的字段。 modelId 不能匹配以下三个模型类型之一:modelReport、modelProposal、modelInternship,我有 getter 和 setter。

标签: hibernate


【解决方案1】:

我猜modelId 是嵌套实体的 id,而不是实体本身,因此我希望是这样的

"SELECT f FROM Formation f WHERE f.model" + modelType + ".ModelId = :modelId")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 2014-10-09
    • 2014-09-28
    • 2015-10-11
    • 2018-01-24
    • 1970-01-01
    相关资源
    最近更新 更多