Announcement Announcement Module
Collapse
No announcement yet.
Simple JPA Query Error Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple JPA Query Error

    Hello developers, I just want to count all customer whose firstname is already in db so use this codes:

    Code:
    public int countCustomer(String firstname) {
    		Query countCust = em
    				.createNamedQuery(
    						"SELECT COUNT(c) FROM Customer c WHERE c.firstname = :firstname")
    				.setParameter(firstname, firstname);
    		return countCust.getMaxResults();
    	}
    Code:
    @SuppressWarnings("unused")
    	@RequestMapping(value = "/addCustomer", method = RequestMethod.POST)
    	private String addCustomerSuccess(
    			@ModelAttribute("customer") @Valid Customer customer,
    			BindingResult result) {
    
    		if (result.hasErrors()) {
    			return "/addCustomer";
    		}
    
    		int count = customerDAO.countCustomer(customer.getFirstname());
    
    		if (count != 0) {
    			customerDAO.save(customer);
    			return "redirect:/customer";
    		} else {
    			return "/addCustomer";
    		}
    
    	}
    but after execution, I got some errors that i can't really understand.

    Code:
    SEVERE: Servlet.service() for servlet [appServlet] in context with path [/ThesisProj] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Named query not found: SELECT COUNT(c) FROM Customer c WHERE c.firstname = :firstname; nested exception is java.lang.IllegalArgumentException: Named query not found: SELECT COUNT(c) FROM Customer c WHERE c.firstname = :firstname] with root cause
    java.lang.IllegalArgumentException: Named query not found: SELECT COUNT(c) FROM Customer c WHERE c.firstname = :firstname
    	at org.hibernate.ejb.AbstractEntityManagerImpl.createNamedQuery(AbstractEntityManagerImpl.java:455)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
    	at $Proxy74.createNamedQuery(Unknown Source)
    	at com.sti.ThesisProj.CustomerDAOImpl.countCustomer(CustomerDAOImpl.java:40)
    	at com.sti.ThesisProj.CustomerDAOImpl$$FastClassByCGLIB$$464f4102.invoke(<generated>)
    	at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
    	at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    	at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
    	at com.sti.ThesisProj.CustomerDAOImpl$$EnhancerByCGLIB$$4a6993fb.countCustomer(<generated>)
    	at com.sti.ThesisProj.CustomerController.addCustomerSuccess(CustomerController.java:46)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    	at com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.traceNextValve(HttpRequestOperationCollectionValve.java:112)
    	at com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.invoke(HttpRequestOperationCollectionValve.java:94)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    	at java.lang.Thread.run(Thread.java:662)
    can someone tell me what i am doing wrong?
    Thanks. BTW i'm Rodel.

  • #2
    Code:
    nested exception is org.springframework.dao.InvalidDataAccessApiUsageException:
    Named query not found: 
    SELECT COUNT(c) FROM Customer c WHERE c.firstname = :firstname; 
    nested exception is java.lang.IllegalArgumentException: 
    Named query not found: 
    SELECT COUNT(c) FROM Customer c WHERE c.firstname = :firstname] 
    with root cause
    java.lang.IllegalArgumentException: 
    Named query not found: SELECT COUNT(c) FROM Customer c WHERE c.firstname = :firstname

    I am not an expert about JPA but is correct you write a SQL query for the method
    createNamedQuery ? I think should be something like createQuery (if exists something closely related with this possible pattern name)

    Comment


    • #3
      Ohh... right. Sorry i didn't noticed it. Tht's right i should use createQuery instead of createNamedQuery.

      Thanks for your help. I highly appreciated it.

      Comment

      Working...
      X