【问题标题】:Like condition is not working in JPA @Query类似条件在 JPA @Query 中不起作用
【发布时间】:2019-07-26 12:09:08
【问题描述】:

我正在使用类似的条件是 JPA,我正面临这个问题。

@Query("select new com.tivo.extract.config.model.DTO(s.SourceId, s.SourceName, t.TvsourceLongName) from MyTelevisionSource t join fetch RCMSource s ON s.SourceId = t.SourceId where s.SourceId LIKE ?1% ")
List<DTO> findFilteredSourceList(String seachInput);

如果我使用s.SourceId 就像%?1% --&gt; %searchInput% -&gt; 一样工作

但是对于s.SourceId LIKE ?1% -&gt; searchInput% -&gt; 它不起作用

SourceId DB 中 Long 类型的列。

我遇到了一个异常

Parameter value [021%] did not match expected type [java.lang.Long (n/a)]; 
nested exception is java.lang.IllegalArgumentException: 
Parameter value [021%] did not match expected type [java.lang.Long (n/a)]

【问题讨论】:

  • 也许SourceId 是一个数字而seachInput 是一些文本...
  • @Zorglube,如果我给 s.SourceId 像 %?1%,它可以工作,你能建议如何使用 s.SourceId 像 ?1%,因为我想要帖子搜索。
  • @mohan,你不能在号码上使用LIKE。在号码上,您可以使用&gt;=&lt;。如果 oyu 想做一些LIKE 你必须将你的号码转换成一个字符串。

标签: java jpa spring-data-jpa


【解决方案1】:

试试CONCAT:

@Query("select new com.tivo.extract.config.model.DTO(s.SourceId, s.SourceName, t.TvsourceLongName)
        from MyTelevisionSource t join fetch RCMSource s ON s.SourceId = t.SourceId 
        where s.SourceId LIKE CONCAT(?1,'%') ")
    List<DTO> findFilteredSourceList(String seachInput);

【讨论】:

  • CONCAT(?1,'%') 总是充当 seachInput% ,但有时我想拥有 %seachInput 并且我也可以拥有 %seachInput% 。对此有何建议??
  • 那你需要以编程方式在开头添加%。
猜你喜欢
  • 1970-01-01
  • 2018-03-20
  • 2021-03-31
  • 2021-10-27
  • 2019-08-03
  • 2018-10-03
  • 2020-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多