java.lang.IllegalArgumentException:名称为[xxx]的参数不存在


我正在使用Spring-data-jpa + hibernate

String login = "AdanaKebap";
String userQuery = "select * from user where username like '%:login%'"; // not working
Query query = entityManager.createNativeQuery(userQuery);
query.setParameter("login", login);
List<Object[]> userObjects = query.getResultList();


我会尝试

String userQuery = "select * from user where username like '%?%'"; // not working
query.setParameter("1", login);

String userQuery = "select * from user where username like '%?1%'"; // not working
query.setParameter("1", login);

String userQuery = "select * from user where username like ':login'"; // not working
query.setParameter("login", login);

String userQuery = "select * from user where username like :login "; // not working
query.setParameter("login", login);

String userQuery = "select * from user where username = :login "; // working, but i don't need this

最佳答案

您需要使用CONCAT来合并%和传递给查询的值,因此请尝试:

String userQuery = "select * from user where username like CONCAT('%', ?1, '%')";
//...
query.setParameter(1, login); // note here you have to use the index of placeholder


要么

String userQuery = "select * from user where username like CONCAT('%', :login, '%')'";
//...
query.setParameter("login", login);

10-08 08:57