Announcement Announcement Module
Collapse
No announcement yet.
org.hibernate.ejb.QueryImpl cannot be cast to com.exampl.standupmanager.domain.Employ Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • org.hibernate.ejb.QueryImpl cannot be cast to com.exampl.standupmanager.domain.Employ

    @RequestMapping(method = RequestMethod.POST, produces = "text/html")
    public String create(@Valid Standup standup, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest, Principal principal, Employee employee) {
    if (bindingResult.hasErrors()) {

    populateEditForm(uiModel, standup);
    return "standups/create";
    }

    uiModel.asMap().clear();


    String name = SecurityContextHolder.getContext().getAuthenticati on().getName();

    Employee empByName = (Employee) Employee.findEmployeesByNameEquals(name);

    System.out.println(empByName.getName());
    standup.persist();
    return "redirect:/standups/" + encodeUrlPathSegment(standup.getId().toString(), httpServletRequest);
    }



    that is the portion of my code. Everything works fine just except the line where I'm trying to create an employee by finding it by it's name. Employee is an active record class and the finder is default. I've just added it.

    The error says:

    org.hibernate.ejb.QueryImpl cannot be cast to com.example.standupmanager.domain.Employee


    and the stack trace:

    com.example.standupmanager.web.StandupController.c reate(StandupController.java:44)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:616)
    org.springframework.web.method.support.InvocableHa ndlerMethod.invoke(InvocableHandlerMethod.java:219 )
    org.springframework.web.method.support.InvocableHa ndlerMethod.invokeForRequest(InvocableHandlerMetho d.java:132)
    org.springframework.web.servlet.mvc.method.annotat ion.ServletInvocableHandlerMethod.invokeAndHandle( ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotat ion.RequestMappingHandlerAdapter.invokeHandleMetho d(RequestMappingHandlerAdapter.java:746)
    org.springframework.web.servlet.mvc.method.annotat ion.RequestMappingHandlerAdapter.handleInternal(Re questMappingHandlerAdapter.java:687)
    org.springframework.web.servlet.mvc.method.Abstrac tHandlerMethodAdapter.handle(AbstractHandlerMethod Adapter.java:80)
    org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:925)
    org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:915)
    org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:822)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:637)
    org.springframework.web.servlet.FrameworkServlet.s ervice(FrameworkServlet.java:796)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290)
    org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    org.springframework.orm.jpa.support.OpenEntityMana gerInViewFilter.doFilterInternal(OpenEntityManager InViewFilter.java:180)
    org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:107)
    org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
    org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 311)
    org.springframework.security.web.access.intercept. FilterSecurityInterceptor.invoke(FilterSecurityInt erceptor.java:116)
    org.springframework.security.web.access.intercept. FilterSecurityInterceptor.doFilter(FilterSecurityI nterceptor.java:83)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 323)
    org.springframework.security.web.access.ExceptionT ranslationFilter.doFilter(ExceptionTranslationFilt er.java:113)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 323)
    org.springframework.security.web.session.SessionMa nagementFilter.doFilter(SessionManagementFilter.ja va:101)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 323)
    org.springframework.security.web.authentication.An onymousAuthenticationFilter.doFilter(AnonymousAuth enticationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 323)
    org.springframework.security.web.servletapi.Securi tyContextHolderAwareRequestFilter.doFilter(Securit yContextHolderAwareRequestFilter.java:54)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 323)
    org.springframework.security.web.savedrequest.Requ estCacheAwareFilter.doFilter(RequestCacheAwareFilt er.java:45)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 323)
    org.springframework.security.web.authentication.http://www.BasicAuthenticationFilter...lter.java:150)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 323)
    org.springframework.security.web.authentication.Ab stractAuthenticationProcessingFilter.doFilter(Abst ractAuthenticationProcessingFilter.java:182)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 323)
    org.springframework.security.web.authentication.lo gout.LogoutFilter.doFilter(LogoutFilter.java:105)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 323)
    org.springframework.security.web.context.SecurityC ontextPersistenceFilter.doFilter(SecurityContextPe rsistenceFilter.java:87)
    org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 323)
    org.springframework.security.web.FilterChainProxy. doFilter(FilterChainProxy.java:173)
    org.springframework.web.filter.DelegatingFilterPro xy.invokeDelegate(DelegatingFilterProxy.java:346)
    org.springframework.web.filter.DelegatingFilterPro xy.doFilter(DelegatingFilterProxy.java:259)
    org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
    org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    org.springframework.web.filter.HiddenHttpMethodFil ter.doFilterInternal(HiddenHttpMethodFilter.java:7 7)
    org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:107)
    org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
    org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    org.springframework.web.filter.CharacterEncodingFi lter.doFilterInternal(CharacterEncodingFilter.java :88)
    org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:107)
    org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
    org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:233)
    org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:191)
    org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:127)
    org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:102)
    org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
    org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:298)
    org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:857)
    org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:588)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run( JIoEndpoint.java:489)
    java.lang.Thread.run(Thread.java:679)



    What am I doing wrong? Thanks in advance.

  • #2
    Originally posted by hasan.iqbal.anik View Post
    Employee empByName = (Employee) Employee.findEmployeesByNameEquals(name);
    Finder returns a javax.persistence.TypedQuery<X> of your entity type, not the entity.
    Get the result list or the single result:

    Code:
    List<Employee> empByName = Employee.findEmployeesByNameEquals(name).getResultList();
    Code:
    Employee empByName = Employee.findEmployeesByNameEquals(name).getSingleResult();
    Regards !

    Comment

    Working...
    X