【发布时间】:2020-11-14 15:07:46
【问题描述】:
在我的 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
【问题讨论】:
-
它没有用
-
然后我怀疑当字符串值为空时会出现问题。你可以在 concat 中使用
COALESCE(:lastName, '') -
不是真的,如果我删除
concat()并替换为upper()或lower()它可以工作 -
你可以试试
concat运算符而不是||: docs.jboss.org/hibernate/orm/5.3/userguide/html_single/… -
@RobertNiestroj Hibernate 文档明确表示不要使用此docs.jboss.org/hibernate/orm/current/userguide/html_single/…
标签: java sql-server spring-boot spring-data-jpa spring-data