0
votes

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

1

1 Answers

0
votes

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?