ECM
ECM
总版主
总版主
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:481回复:0

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.spring.webapp.lojavirtual.ac

楼主#
更多 发布于:2016-05-12 14:42
In my spring project, I have implemented a generic class to serve as base for all my Dao class. By example, my class UsuarioHome have this code: @Repository
public class UsuarioHome extends Dao<Usuario> {

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

}
 and my generic class has this code: public class Dao<E> {

    private final E entity;

    @Autowired
    SessionFactory sessionFactory;

    protected Session getCurrentSession(){
        return sessionFactory.getCurrentSession();
    }

    public Dao(E entity) {  
        this.entity = entity;
    }

    @SuppressWarnings("unchecked")
    public Dao(Class<?> classe) {
        this.entity = (E) classe;
    }

    public E getEntity() {
        return this.entity;
    }

    @Transactional
    public boolean persist(E transientInstance) {
        try {
            sessionFactory.getCurrentSession().persist(transientInstance);
            return true;
        } catch (RuntimeException re) {
            return false;
        }
    }

    @Transactional
    public boolean remove(E transientInstance) {
        try {
            sessionFactory.getCurrentSession().delete(transientInstance);
            return true;
        } catch (RuntimeException re) {
            return false;
        }
    }

    @Transactional
    public boolean merge(E detachedInstance) {
        try {
            sessionFactory.getCurrentSession().merge(detachedInstance);
            return true;
        } catch (RuntimeException re) {
            return false;
        }
    }

    @SuppressWarnings("unchecked")
    @Transactional
    public E findById(int id) {
        try {
            E instance = (E) sessionFactory.getCurrentSession().get(entity.getClass(), id);
            return instance;
        } catch (RuntimeException re) {
            return null;
        }
    }

    @SuppressWarnings("unchecked")
    @Transactional
    public E findByField(String field, String value) {
        try {
            String expressao = entity.toString();
            String nome_classe = new String();
            StringTokenizer st = new StringTokenizer(expressao);
            while (st.hasMoreTokens()) {
                nome_classe = st.nextToken();
            }
            String query = "from "+nome_classe+" where "+field+" = :data";

            Query q = sessionFactory.getCurrentSession().createQuery(query);
            q.setParameter("data", value);
            E instance = (E) q.uniqueResult();
            return instance;
        } catch (RuntimeException re) {
            return null;
        }
    }

    @SuppressWarnings("unchecked")
    @Transactional
    public List<E> findAll() {
        try {
            List<E> instance = sessionFactory.getCurrentSession().createCriteria(entity.getClass()).list();
            return instance;
        } catch (RuntimeException re) {
            return null;
        }
    }

}
 But, when I need retrieve a list of all items from the table, I am receiving 0 items. The method for do this is placed in my class UsuarioService: @Transactional
public List<Usuario> listagem_usuarios() {
    return usuario.findAll();
}
 where I autowires my Dao class: @Autowired
private UsuarioHome usuario;

@Autowired
private GrupoPermissaoHome grupo_permissao;
 Anyone can tell what I am doing wrong here? I tested this solution in other projects (simpler than that), and it work. UPDATE When I try run the application, I am facing a cast error, like this, by example, when this json page is opened: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>
<json:object>
<c:forEach var="item" items="${lista}">
      <json:object name="usuario">
         <json:property name="id" value="${item.id}"/>
         <json:property name="login" value="${item.login}"/>
         <json:property name="pnome" value="${item.first_name}"/>
         <json:property name="unome" value="${item.last_name}"/>
         <json:property name="email" value="${item.email}"/>
      </json:object>
</c:forEach>
</json:object>
 the console shows this error: Grave: Servlet.service() for servlet jsp threw exception
java.lang.NumberFormatException: For input string: "id"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at javax.el.ArrayELResolver.coerce(ArrayELResolver.java:166)
    at javax.el.ArrayELResolver.getValue(ArrayELResolver.java:46)
    at org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
    at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp._jspx_meth_json_005fproperty_005f0(listagem_005fusuarios_jsp.java:168)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp.access$1(listagem_005fusuarios_jsp.java:156)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp$Helper.invoke1(listagem_005fusuarios_jsp.java:271)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp$Helper.invoke(listagem_005fusuarios_jsp.java:310)
    at atg.taglib.json.JsonObjectTag.doTag(JsonObjectTag.java:63)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp._jspx_meth_json_005fobject_005f1(listagem_005fusuarios_jsp.java:151)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp._jspx_meth_c_005fforEach_005f0(listagem_005fusuarios_jsp.java:117)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp.access$0(listagem_005fusuarios_jsp.java:98)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp$Helper.invoke0(listagem_005fusuarios_jsp.java:261)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp$Helper.invoke(listagem_005fusuarios_jsp.java:307)
    at atg.taglib.json.JsonObjectTag.doTag(JsonObjectTag.java:63)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp._jspx_meth_json_005fobject_005f0(listagem_005fusuarios_jsp.java:93)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp._jspService(listagem_005fusuarios_jsp.java:68)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:263)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:146)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

Mai 20, 2014 7:40:22 AM org.apache.catalina.core.StandardWrapperValve invoke
Grave: Servlet.service() for servlet [json_dispatcher] in context with path [/loja] threw exception [An exception occurred processing JSP page /WEB-INF/view/json/listagem_usuarios.jsp at line 5

2: <%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>
3: <json:object>
4: <c:forEach var="item" items="${lista}">
5:    <json:object name="usuario">
6:       <json:property name="id" value="${item.id}"/>
7:       <json:property name="login" value="${item.login}"/>
8:       <json:property name="pnome" value="${item.first_name}"/>


Stacktrace:] with root cause
java.lang.NumberFormatException: For input string: "id"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at javax.el.ArrayELResolver.coerce(ArrayELResolver.java:166)
    at javax.el.ArrayELResolver.getValue(ArrayELResolver.java:46)
    at org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
    at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp._jspx_meth_json_005fproperty_005f0(listagem_005fusuarios_jsp.java:168)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp.access$1(listagem_005fusuarios_jsp.java:156)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp$Helper.invoke1(listagem_005fusuarios_jsp.java:271)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp$Helper.invoke(listagem_005fusuarios_jsp.java:310)
    at atg.taglib.json.JsonObjectTag.doTag(JsonObjectTag.java:63)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp._jspx_meth_json_005fobject_005f1(listagem_005fusuarios_jsp.java:151)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp._jspx_meth_c_005fforEach_005f0(listagem_005fusuarios_jsp.java:117)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp.access$0(listagem_005fusuarios_jsp.java:98)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp$Helper.invoke0(listagem_005fusuarios_jsp.java:261)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp$Helper.invoke(listagem_005fusuarios_jsp.java:307)
    at atg.taglib.json.JsonObjectTag.doTag(JsonObjectTag.java:63)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp._jspx_meth_json_005fobject_005f0(listagem_005fusuarios_jsp.java:93)
    at org.apache.jsp.WEB_002dINF.view.json.listagem_005fusuarios_jsp._jspService(listagem_005fusuarios_jsp.java:68)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:263)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:146)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
 UPDATE 2 After more tests, I verify that when I use: List<E> instance = sessionFactory.getCurrentSession().createCriteria(entity.getClass()).list();
 I get an empty list. But when I use: List<E> instance = (List<E>) sessionFactory.getCurrentSession().createSQLQuery("select * from usuario").list();
 I get the data from database, but I can access them, due to an cast error (explained above in the first update) ps.: the other methods, findById and findByField, are working fine.Ok, I solve this issue following the sugestion from this topic: Class derived from generic class don't get the correct type the final code for the Dao template it's that: public class Dao<E> {

    private final Class<E> entity;

    @Autowired
    protected SessionFactory sessionFactory;

    protected Session getCurrentSession(){
        return sessionFactory.getCurrentSession();
    }

    /*public Dao(E entity) {  
        this.entity = entity;
    }*/

    public Dao(Class<E> classe) {
        this.entity = classe;
    }

    /*public E getEntity() {
        return this.entity;
    }*/

    @Transactional
    public boolean persist(E transientInstance) {
        sessionFactory.getCurrentSession().persist(transientInstance);
        return true;
    }

    @Transactional
    public boolean remove(E transientInstance) {
        sessionFactory.getCurrentSession().delete(transientInstance);
        return true;
    }

    @Transactional
    public boolean merge(E detachedInstance) {
        sessionFactory.getCurrentSession().merge(detachedInstance);
        return true;
    }

    @Transactional
    public E findById(int id) {
        E instance = (E) sessionFactory.getCurrentSession().get(entity.getClass(), id);
        return instance;
    }

    @Transactional
    public E findByField(String field, String value) {
        String expressao = entity.toString();
        String nome_classe = new String();
        StringTokenizer st = new StringTokenizer(expressao);
        while (st.hasMoreTokens()) {
            nome_classe = st.nextToken();
        }
        String query = "from "+nome_classe+" where "+field+" = :data";

        Query q = sessionFactory.getCurrentSession().createQuery(query);
        q.setParameter("data", value);
        E instance = (E) q.uniqueResult();
        return instance;
    }

    @Transactional
    public List<E> findAll() {
        return (List<E>) sessionFactory.getCurrentSession().createCriteria(entity).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
    }

}

 

 

 

 

 

 

 

异常中心网是一家专门收集整理程序员编程过程中遇到的常见异常(exception)以及各种异常问答中心的网站。异常中心网旨在,减少程序员在编码遇到异常,处理各种异常时间和痛苦,让程序员能更愉快的、快速的定位异常并查找对应的异常解决方案。异常中心网诚心打造最完美的编程社区为程序员用户服务,努力成为最好的程序员乐园程序员社区程序异常中心程序bug中心异常问答中心

 

喜欢0 评分0
游客

返回顶部