[英]Spring Data and native query with like statement
I have the following repository definition:我有以下存储库定义:
public interface InstructionRepository extends JpaRepositoryWithSpecification<Instruction> {
String FIND_INSTRUCTIONS_BY_TRACKING_ID_QUERY =
"SELECT * FROM instruction i WHERE i.invoice_documents_trackings like '%:trackingId%'";
@Query(value = FIND_INSTRUCTIONS_BY_TRACKING_ID_QUERY, nativeQuery = true)
List<Instruction> findByFileTrackingsContaining(@Param("trackingId") String trackingId);
}
The reason why I use native query here is because invoice_documents_trackings
column represents a map serialized to json string.我在这里使用原生查询的原因是
invoice_documents_trackings
列代表一个 map 序列化为 json 字符串。 So basically I want to find all instructions that have particular trackingId
stored in the invoice_documents_trackings
map.所以基本上我想找到所有在
invoice_documents_trackings
map 中存储了特定trackingId
的指令。
When I execute the method I always get 0 results despite the fact that If I execute the same query manually I get expected results.当我执行该方法时,我总是得到 0 结果,尽管如果我手动执行相同的查询,我会得到预期的结果。
I also tried to change the query so that it looks like:我还尝试更改查询,使其看起来像:
String FIND_INSTRUCTIONS_BY_TRACKING_ID_QUERY =
"SELECT * FROM instruction i WHERE i.invoice_documents_trackings like %:trackingId%"
And this does not work either.这也不起作用。
Would really appreciate any help, than真的很感激任何帮助,而不是
I think the issue is the way you're using %
我认为问题在于您使用
%
的方式
String FIND_INSTRUCTIONS_BY_TRACKING_ID_QUERY =
"SELECT * FROM instruction i WHERE i.invoice_documents_trackings like CONCAT('%', :trackingId, '%')"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.