SQL Server Spring数据JPA:数据类型varchar和varbinary在使用concat()方法时发生的add运算符异常中不兼容

btxsgosb  于 2023-01-12  发布在  Spring
关注(0)|答案(2)|浏览(316)

在我的Spring Data项目中,我正尝试在JPA查询注解上使用concat操作(如下所示)

@Query("SELECT a from ApiEnrollmentsView a where (:mrn is null or a.mrn=:mrn) and " +
        " (:firstName is null  or a.firstName like concat(:firstName, '%')) and " +
        " (:lastName is null  or a.lastName like concat(:lastName, '%')) ")
List<ApiEnrollmentsView> findEnrollmentsByMrnAndFirstNameAndLastName(String mrn, String firstName, String lastName);

项目编译正常,但当我访问该方法时,遇到异常data types varchar and varbinary are incompatible in the add operator

例外情况:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The data types varchar and varbinary are incompatible in the add operator.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:262)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1632)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:600)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:522)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7225)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:3053)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:247)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:222)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:444)
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:57)
    ... 174 more
68bkxrlz

68bkxrlz1#

我记得我也遇到过同样的奇怪错误。正如评论中提到的,这个问题是通过使用coalesce函数而不是concat解决的。

ia2d9nvy

ia2d9nvy2#

CONCAT中使用COALESCE(:lastName, '')。它解决了我的问题

相关问题