Jdbi: @Bind 和 @BindBean 的奇怪行为

创建于 2020-11-10  ·  3评论  ·  资料来源: jdbi/jdbi

大家好,

我不知道这是否已经发布,因为我没有找到任何东西。 如果是这样,我很抱歉打扰你们。

我们在我们的团队中注意到以下情况的奇怪行为。
我们的 JDBI 看起来像

public interface DeviceRepository
{
    @RegisterBeanMapper(DeviceRowMapper.class)
    @SqlQuery(
            "SELECT * FROM myDeviceTable " +
            "WHERE name = :propertyEight " +
            "      AND devicePropertyOne = :propertyOne " +
            "      AND devicePropertyTwo = :propertyTwo " +
            "      AND devicePropertyThree =: propertyThree " +
            "      AND devicePropertyFour =: propertyFour"
            "      AND devicePropertyFive =: propertyFive"
            "      AND devicePropertySix =: propertySix"
            "      AND devicePropertySeven =: propertySeven"
            ")
      List<Device> findAllCurrentWith(
            <strong i="9">@Bind</strong> String propertypropertyOneTwo,
            <strong i="10">@Bind</strong> String propertypropertyThreeTwo,
            <strong i="11">@Bind</strong> String propertyFour,
            <strong i="12">@BindBean</strong> DeviceBean deviceBean
      );
}

DeviceBean类看起来像

<strong i="17">@Data</strong>
public final class DeviceRequestDto
{
    private String propertyTwo;
    private String propertyFive;
    private String propertySix;
    private String propertySeven;
    private String propertyEight;
}

在我的机器上一切正常,在测试它时,在我们的管道中,测试全部通过,因为它很好。

但这里有一个问题,在我们的一名团队成员机器上,测试或更具体的 SQL 语句没有执行,因为绑定的异常被抛出。

2020-11-10 10:54:51,542 [http-nio-8081-exec-4] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [/device] threw exception [Request processing failed; nested exception is org.jdbi.v3.core.statement.UnableToCreateStatementException: Missing named parameter 'propertyOne' in binding:{​​​​​​​positional:{​​​​​​​0:propertyOne,1:propertyThree,2:propertyFour}​​​​​​​, named:{​​​​​​​}​​​​​​​, finder:[{​​​​​​​lazy bean property arguments "DeviceBean(...)"}​​​​​​​]}​​​​​​​ [statement:SELECT * FROM myDeviceTable WHERE name = :customerNumber AND devicePropertyOne = :propertyOne AND devicePropertyTwo = :propertyTwo AND devicePropertyThree =: propertyThree AND devicePropertyFour =: propertyFour", arguments:{​​​​​​​positional:{​​​​​​​0:propertyOne,1:propertyThree,2:propertyFour}​​​​​​​​​​​​​​, named:{​​​​​​​}​​​​​​​, finder:[{​​​​​​​lazy bean property arguments "DeviceBean(...)"}​​​​​​​]}​​​​​​​]] with root cause
org.jdbi.v3.core.statement.UnableToCreateStatementException:  Missing named parameter 'propertyOne' in binding:{​​​​​​​positional:{​​​​​​​0:propertyOne,1:propertyThree,2:propertyFour}​​​​​​​, named:{​​​​​​​}​​​​​​​, finder:[{​​​​​​​lazy bean property arguments "DeviceBean(...)"}​​​​​​​]}​​​​​​​ [statement:SELECT * FROM myDeviceTable WHERE name = :customerNumber AND devicePropertyOne = :propertyOne AND devicePropertyTwo = :propertyTwo AND devicePropertyThree =: propertyThree AND devicePropertyFour =: propertyFour", arguments:{​​​​​​​positional:{​​​​​​​0:propertyOne,1:propertyThree,2:propertyFour}​​​​​​​​​​​​​​, named:{​​​​​​​}​​​​​​​, finder:[{​​​​​​​lazy bean property arguments "DeviceBean(...)"}​​​​​​​]}​​​​​​​]
    at org.jdbi.v3.core.statement.ArgumentBinder.missingNamedParameter(ArgumentBinder.java:147)

因此,第一个直觉是另外将 sql 参数绑定到@Bind注释。

  • <strong i="30">@Bind</strong> String propertyOne之前
  • <strong i="33">@Bind</strong> String("propertyOne") propertyOne之后。

然后我同事机器上的异常消失了。 不完全确定这个问题是否来自我们这边,但是看到它在我的机器和管道上传递有点奇怪,但是从我同事机器上执行的新主分支失败。

我只是想让你知道是否有可能有一点改进。

所有3条评论

仅当您启用将参数名称写入类文件的编译器选项时,使用@Bind而不指定列名才有效,Jdbi 可以将其读出。

听起来您的同事需要打开此功能。 请在此处阅读:http: //jdbi.org/#_compiling_with_parameter_names

另请注意,当使用-parameters编译器选项时,$ @Bind注释变为可选。

好的,感谢您澄清这个@qualidafial

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

keith-miller picture keith-miller  ·  3评论

buremba picture buremba  ·  5评论

qualidafial picture qualidafial  ·  3评论

jimmyhmiller picture jimmyhmiller  ·  6评论

raderio picture raderio  ·  6评论