简体   繁体   中英

Bind parameter to a native query annotation JPA

I have the following piece of code:

@Query(value = "select * from james_mail where encode(header_bytes, 'escape') like '%Message-ID: :messageId%'", nativeQuery = true)
    List<JamesMail> findByMessageIdFromHeader(@Param(value = "messageId") String messageId);

The expected behavior is to execute the query with bind parameter (eg messageID)

Actual behavior is that the query is executed as it is, without biding

What is the solution for actually biding, or there is an workaround for my problem?

Technology stack: Spring Boot, JPA, Hibernate

here you are trying to encode header_bytes which is contained table james_mail right and you want to find rows which contains value matching to messageId and contains Message-ID

@Query(value = "select * from james_mail where encode(header_bytes, 'escape')   like '%Message-ID%' and encode(header_bytes, 'escape') like %:messageId%", nativeQuery = true)
List<JamesMail> findByMessageIdFromHeader(@Param(value = "messageId") String messageId);

Try to correct your query in this way:

@Query(value = "select * from james_mail where encode(header_bytes, 'escape') like '%Message-ID: ' || :messageId || '%'", nativeQuery = true)
List<JamesMail> findByMessageIdFromHeader(@Param(value = "messageId") String messageId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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