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.contex
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…