Error Returning Query Using Like

Asked

Viewed 50 times

0

I am trying to make a jpql query with like using JPA. However, it is not working.

User class repository

public class UsuarioRepository extends RepositorioGenerico<Integer, Usuario> {

    public UsuarioRepository() {
        super(Usuario.class);
    }

  public List<Usuario> porNome(String nome) {

        String jpql = "SELECT u FROM Usuario u WHERE u.nome like ? ";

        return find(jpql, nome);
    }

}

Method responsible for consultation on Repositoryogeneric.

public List<T> find(String jpql, Object... params) {
        entityManager.getTransaction().begin();
        Query query = entityManager.createQuery(jpql);

        for (int i = 0; i < params.length; i++) {
            query.setParameter(i + 1, params[i]);
        }

        List<T> entities = query.getResultList();

        entityManager.getTransaction().commit();
        entityManager.close();

        return entities;
    }

The query generates the following error:

java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing [SELECT u FROM Usuario u WHERE u.nome like ? ]. 
[42, 43] The parameter value is missing from the input parameter.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)
    at com.rafael.easygasws.repositorios.RepositorioGenerico.find(RepositorioGenerico.java:71)
    at com.rafael.easygasws.repositorios.UsuarioRepository.porNome(UsuarioRepository.java:46)
    at com.rafael.easygasws.rest.UsuarioREST.usuarioPorNome(UsuarioREST.java:82)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

SOLUTION

I decided to change the method porNome()

 public List<Usuario> porNome(String nome) {
    String jpql = "select u from Usuario u where u.nome like :nome ";
    TypedQuery<Usuario> query = getEntityManager().createQuery(jpql, Usuario.class);

    query.setParameter("nome", "%" + nome + "%");

    List<Usuario> entitades = query.getResultList();

    return entitades;

}

1 answer

1

Place the parameter number after the question mark.

Example:

SELECT u FROM User u WHERE u.name like ? 1

  • Now the result is coming empty.

  • Checks whether the query actually finds something with the name passed by parameter.

  • Yes, you do. I used the query directly in the database and brought results.

  • Got it, you came to debug on this line: List<T> entities = query.getResultList(); ? Here it’s coming empty?

  • So, it’s returning empty even. I’ve even changed Query for TypedQuery<T> query = entityManager.createQuery(jpql, classePersistente);. You still haven’t solved.

  • I managed to solve by changing the Class method UsuarioRepository. I edited my asking by inserting the solution. Anyway, thank you so much for the help!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.