Spring Data JPA: The data types varchar and varbinary are incompatible in the add operator exception occurs when using concat() method

huangapple go评论76阅读模式
英文:

Spring Data JPA: The data types varchar and varbinary are incompatible in the add operator exception occurs when using concat() method

问题

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

@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);

项目编译正常,但当我访问该方法时,我收到异常信息:“数据类型varchar和varbinary在加法运算符中不兼容”。

异常信息:

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)
	...
英文:

In my Spring Data project I am trying to use concat operation on JPA query annotation(shown below)

@Query(&quot;SELECT a from ApiEnrollmentsView a where (:mrn is null or a.mrn=:mrn) and &quot; +
        &quot; (:firstName is null  or a.firstName like concat(:firstName, &#39;%&#39;)) and &quot; +
        &quot; (:lastName is null  or a.lastName like concat(:lastName, &#39;%&#39;)) &quot;)
List&lt;ApiEnrollmentsView&gt; findEnrollmentsByMrnAndFirstNameAndLastName(String mrn, String firstName, String lastName);

Project compiles fine but when I access the method, I get exception data types varchar and varbinary are incompatible in the add operator

Exception:

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

答案1

得分: 2

我记得我曾经遇到过完全相同的奇怪错误。正如评论中提到的,问题是通过使用 coalesce 函数而不是 concat 得以解决。

英文:

I remember I had exactly the same weird error. As it is mentioned in the comments the problem solved by using coalesce function instead of concat.

答案2

得分: 0

使用 COALESCE(:lastName, '') 替代 CONCAT 函数内部。这解决了我的问题。

英文:

use COALESCE(:lastName, &#39;&#39;) inside CONCAT. It resolves my issue

huangapple
  • 本文由 发表于 2020年7月25日 01:43:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63078835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定