【问题标题】:Hibernate: Parameter value did not match expected type?Hibernate:参数值与预期类型不匹配?
【发布时间】:2016-07-13 14:39:37
【问题描述】:

当我尝试运行时:

List<MappingInstruction> result = emFactory.get()
          .createQuery("FROM " + MappingInstruction.class.getSimpleName() + " c where c.mapping = :mapping",
              MappingInstruction.class).setParameter("mapping", mappingInfo.getId()).getResultList();

我明白了:

服务异常执行动作“MappingAddAction”, java.lang.IllegalArgumentException: 参数值 [5118] 没有 匹配预期类型 [com.vernuso.trust.server.domain.clientimport.MappingInfo (n/a)]

我在 Eclipse 中调试,mappingInfo.getId() 确实返回 5118。

有人能帮我理解为什么它需要MappingInfo而不是long吗?

我有两个表,如下图所示。 MappingInfo 与 MappingInstruction 表是一对多的关系。

【问题讨论】:

    标签: hibernate


    【解决方案1】:

    仔细看:

    List<MappingInstruction> result = emFactory.get()
              .createQuery("FROM " + MappingInstruction.class.getSimpleName() + " c where c.mapping = :mapping",
                  MappingInstruction.class).setParameter("mapping", mappingInfo.getId()).getResultList();
    

    您正在从 MappingInstruction 实体中获取。在 where 条件下,c.mapping 是 MappingInfo 类型。请检查 MappingInstruction 类中映射变量的数据类型。

    因为 c.mapping 属于 MappingInfo 类型,但您传递的是很长的 mappingInfo.getId(),即 ava.lang.IllegalArgumentException强>投掷。

    所以,更正如下:

    List<MappingInstruction> result = emFactory.get()
                  .createQuery("FROM " + MappingInstruction.class.getSimpleName() + " c where c.mapping = :mapping",
                      MappingInstruction.class).setParameter("mapping", mappingInfo).getResultList();
    

    【讨论】:

    • 谢谢伙计。我过于关注表中的值,即 5118。如果你能回答,只是一个附带问题:如何从“结果”中删除一个项目?我试过了 result.remove(executionOrder);但它似乎不起作用。
    • result.remove(executionOrder) 不起作用!!!如果您只想从列表中删除,而不是从数据库中删除,您可以将列表复制到另一个列表中的另一个列表,然后尝试从该新列表中删除。
    • 我想从数据库中删除该条目。因为,我已经从 Db 中获取了结果列表,我希望从这个列表中删除也能反映 DB 中的变化。
    猜你喜欢
    • 2013-02-20
    • 2019-02-11
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2018-09-02
    • 2016-04-22
    • 2020-11-29
    • 2015-12-13
    相关资源
    最近更新 更多