【问题标题】:Parameter value [0] did not match expected type [java.lang.Integer]参数值 [0] 与预期类型 [java.lang.Integer] 不匹配
【发布时间】:2015-10-20 20:24:38
【问题描述】:

有趣的问题是我传递了一个 int 并且它抱怨它与类型不匹配:

org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [0] did not match expected type [java.lang.Integer]

@Procedure(procedureName = "dbo.do_cool_stuff_to_client")
void coolClientStuff(int clientId);

它是这样称呼的:

public void someOtherMethod(int clientId){
  clientRepository.coolClientStuff(clientId);
}

【问题讨论】:

    标签: java spring jpa stored-procedures hql


    【解决方案1】:

    事实证明它有些愚蠢/时髦,它真的希望我放入类类型而不是原始类型。

    将方法签名更改为使用 Integer 而不是 int 已修复此问题。

    @Procedure(procedureName = "dbo.do_cool_stuff_to_client")
    void coolClientStuff(Integer clientId);
    

    【讨论】:

    • 阅读expected type [java.lang.Integer]后,我会期望参数是Integer。是否还有其他@Procedurecase 可以使用 int 代替 Integer?我对这个注释不熟悉
    • 这很奇怪;这真的不重要,因为自动装箱应该处理它。也许当 Spring 在这里使用反射时,它没有考虑使用原语而不是包装器类型的可能性?它可能正在检查参数的类是否仅为== Integer.class 而没有考虑int.class
    • 是的,异常让我切换它并让它工作,这很奇怪,因为我原以为它对原始类型没有问题,因为这是自始至终都在使用的。
    猜你喜欢
    • 2019-09-03
    • 2019-07-23
    • 1970-01-01
    • 2021-08-05
    • 2016-07-13
    • 1970-01-01
    • 2018-09-02
    • 2016-04-22
    • 2020-11-29
    相关资源
    最近更新 更多