简体   繁体   中英

to put some MYSQL to a named query

i need to translate some MySQL code to a JPA named query

example

select * from movies where title like '%matrix%'

would my gues it would

 @NamedQuery(name = "Movies.findMoviePart", query = "SELECT c FROM Movies c where c.title LIKE '% :title %'")}) be but it has some errors 

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryParameterException: could not locate named parameter [title]; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: could not locate named parameter [title]

code in dao impl

@Override public List getAllPartMovie(String title) {
TypedQuery query = entityManager.createNamedQuery("Movies.findMoviePart", Movies.class); query.setParameter("title", title); try { return query.getResultList(); } catch (NoResultException ex) { // geen record gevonden return null; }

tanks for your help guys

Don't add the % to the query, add them to the parameter in your query:

query.setParameter("title", "%" + value + "%");

Another question almost identical to yours: How to specify a JPA named parameter surrounded by wildcards?

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