简体   繁体   English

将一些MYSQL放入命名查询

[英]to put some MYSQL to a named query

i need to translate some MySQL code to a JPA named query 我需要将一些MySQL代码转换为JPA命名查询

example

select * from movies where title like '%matrix%' 从标题为“%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]; org.springframework.dao.InvalidDataAccessApiUsageException:org.hibernate.QueryParameterException:找不到命名参数[title]; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: could not locate named parameter [title] 嵌套异常是java.lang.IllegalArgumentException:org.hibernate.QueryParameterException:找不到命名参数[title]

code in dao impl dao impl中的代码

@Override public List getAllPartMovie(String title) { @Override公共列表getAllPartMovie(字符串标题){
TypedQuery query = entityManager.createNamedQuery("Movies.findMoviePart", Movies.class); TypedQuery查询= entityManager.createNamedQuery(“ Movies.findMoviePart”,Movies.class); query.setParameter("title", title); query.setParameter(“ title”,title); try { return query.getResultList(); 尝试{return query.getResultList(); } catch (NoResultException ex) { // geen record gevonden return null; } catch(NoResultException ex){// geen记录gevonden返回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? 另一个与您几乎相同的问题: 如何指定一个由通配符包围的JPA命名参数?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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