简体   繁体   English

带有本机查询和有序参数的查询字符串,sql 不起作用

[英]Query String with native query and ordered parameter, sql doesn't work

I have the following code我有以下代码

  @Query(nativeQuery = true, value = "select  e.* from event e"
  + " join league  l on (l.id = e.league_id)"
  + " join sport s on (l.sport_id = s.id)"
  + " join team t1 on (t1.id = e.team_one_id)"
  + " join team t2 on (t2.id = e.team_two_id)"
  + " join country c on (c.id = l.country_id)"
  + " where l.name LIKE %?1%")

Page<Event> getAllEventsFiltered(final PageRequest of, final String filter);

If execute this SQL into database I receive a lot of results but when is executed from java I receive no one result in the same SQL.如果将这个 SQL 执行到数据库中,我会收到很多结果,但是当从 java 执行时,我没有收到相同的 SQL 中的任何结果。

I already tried:我已经尝试过:

where l.name LIKE %?#{escape([1])} escape ?#{escapeCharacter()}

but just works to begins and I needs for both begin and end.但只是开始工作,我需要开始和结束。

JDBC parameters can only be included in very specific places in a SQL statement. JDBC 参数只能包含在 SQL 语句中非常具体的地方。 They are not replaced as strings in the SQL statement but they are applied .在 SQL 语句中,它们没有被替换为字符串,但是它们被应用了。 The specific valid places change from database to database, and also depend on the JDBC driver variant/version.具体有效位置因数据库而异,也取决于 JDBC 驱动程序变体/版本。

The general rule is that a parameter can be applied where a single value would be present.一般规则是,可以在存在单个值的地方应用参数。 String parameters should not be enclosed in single quotes as a VARCHAR would be.字符串参数不应像VARCHAR那样用单引号引起来。

One option to make it compliant, is to place the parameter isolated from other text, for example by using the CONCAT() function. Your query could be rephrased as:使其合规的一种选择是将参数与其他文本隔离开来,例如使用CONCAT() function。您的查询可以改写为:

  @Query(nativeQuery = true, value = "select  e.* from event e"
  + " join league  l on (l.id = e.league_id)"
  + " join sport s on (l.sport_id = s.id)"
  + " join team t1 on (t1.id = e.team_one_id)"
  + " join team t2 on (t2.id = e.team_two_id)"
  + " join country c on (c.id = l.country_id)"
  + " where l.name LIKE concat('%', ?1, '%')") // change here

Nevertheless, the specific syntax change can be different depending on the database you are using (that you haven't mentioned).尽管如此,具体的语法更改可能会有所不同,具体取决于您使用的数据库(您没有提到)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM