简体   繁体   中英

Hibernate not Injecting Parameters in Query Annotation

I've read the documentation on inserting method parameters into queries and other questions however they all suggest that this should work.

 @Query(value = "SELECT * FROM period WHERE time LIKE \":day%\" AND id IN (SELECT id FROM booking)", nativeQuery = true)
    List<Booking> findAllBookingsOnDay(@Param("day") String day);

Results in:

Hibernate: SELECT * FROM period WHERE time LIKE ":day%" AND id IN (SELECT id FROM booking)

I've tried removing quotations and percentage but that just results in.

Hibernate: SELECT * FROM period WHERE time LIKE ? AND id IN (SELECT id FROM booking)

The correct way to do this is to just use an unescaped placeholder in the query, and the bind the LIKE wildcard expression to it from the Java side.

@Query(value = "SELECT * FROM period WHERE time LIKE :day AND id IN (SELECT id FROM booking)", nativeQuery =true)
List<Booking> findAllBookingsOnDay(@Param("day") String day);

Usage:

String day = "%friday%";   // or whatever value would make sense here
findAllBookingsOnDay(day);

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