在我的spring应用程序中,我使用json-taglibs来基于我从数据库中检索的实体列表并在我的控制器中添加请求来动态生成json文件。我正在使用其中一个文件面临此错误:
Grave: Servlet.service() for servlet [json_dispatcher] in context with path [/loja] threw exception [Request processing failed; nested exception is org.springframework.orm.hibernate4.HibernateSystemException: could not get a field value by reflection getter of com.spring.webapp.lojavirtual.common.persistence.model.Categoria.id; nested exception is org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.spring.webapp.lojavirtual.common.persistence.model.Categoria.id] with root cause
java.lang.IllegalArgumentException: Can not set java.lang.Integer field com.spring.webapp.lojavirtual.common.persistence.model.Categoria.id to java.lang.String
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:55)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
at java.lang.reflect.Field.get(Field.java:379)
at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:57)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:346)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4746)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4465)
at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:243)
at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:293)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:537)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:174)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:67)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:613)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1900)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1861)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
at org.hibernate.loader.Loader.doQuery(Loader.java:909)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
at org.hibernate.loader.Loader.doList(Loader.java:2553)
at org.hibernate.loader.Loader.doList(Loader.java:2539)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
at org.hibernate.loader.Loader.list(Loader.java:2364)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
at com.spring.webapp.lojavirtual.config.hibernate.Dao.findByField(Dao.java:67)
at com.spring.webapp.lojavirtual.config.hibernate.Dao$$FastClassBySpringCGLIB$$75e094d4.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:711)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
at com.spring.webapp.lojavirtual.produto.persistence.ProdutoDao$$EnhancerBySpringCGLIB$$b13352aa.findByField(<generated>)
at com.spring.webapp.lojavirtual.produto.service.ProdutoService.listagem_por_categoria(ProdutoService.java:105)
at com.spring.webapp.lojavirtual.produto.service.ProdutoService$$FastClassBySpringCGLIB$$5e9076b4.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:711)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
at com.spring.webapp.lojavirtual.produto.service.ProdutoService$$EnhancerBySpringCGLIB$$c1e4b579_2.listagem_por_categoria(<generated>)
at com.spring.webapp.lojavirtual.produto.controller.ProdutoControler.listagem_por_categoria_json(ProdutoControler.java:100)
at com.spring.webapp.lojavirtual.produto.controller.ProdutoControler$$FastClassBySpringCGLIB$$d626f2a4.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:640)
at com.spring.webapp.lojavirtual.produto.controller.ProdutoControler$$EnhancerBySpringCGLIB$$e007cea4_3.listagem_por_categoria_json(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
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)
出现此错误的文件是:
<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>
<json:object>
<json:array name="produto" var="item" items="${lista}">
<json:object>
<json:property name="id" value="${item.id}"/>
<json:property name="nome" value="${item.nome}"/>
<json:property name="categoria" value="${item.categoria}"/>
</json:object>
</json:array>
</json:object>
来自映射该文件的控制器的方法是:
@RequestMapping(value="listagem.json")
public ModelAndView listagem_json() {
ModelAndView mav = new ModelAndView();
mav.addObject("lista", produto.listagem());
mav.setViewName("listagem_produto");
return mav;
}
@RequestMapping(value="listagem_por_categoria.json", method=RequestMethod.GET)
public ModelAndView listagem_por_categoria_json(HttpServletRequest request, HttpServletResponse response) {
String id_categoria = request.getParameter("id");
ModelAndView mav = new ModelAndView();
if(id_categoria == null)
mav.addObject("lista", produto.listagem());
else
mav.addObject("lista", produto.listagem_por_categoria(id_categoria));
mav.setViewName("listagem_produto");
return mav;
}
注意:只有在我调用第二种方法时才会触发错误。我的服务类中的方法listagem()和listagem_por_categoria()以这种方式实现:
@Transactional
public List<Produto> listagem() {
return produto.findAll();
}
@Transactional
public Produto listagem(int id) {
return produto.findById(id);
}
@Transactional
public List<Produto> listagem_por_categoria(String id_categoria) {
return produto.findByField("categoria", id_categoria);
}
在我的DAO类中,方法findAll(),findById()和findByField()以这种方式实现:
@SuppressWarnings("unchecked")
@Transactional
public E findById(int id) {
E instance = (E) sessionFactory.getCurrentSession().get(entity, id);
return instance;
}
@SuppressWarnings("unchecked")
@Transactional
public List<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);
List<E> instance = q.list();
return instance;
}
@SuppressWarnings("unchecked")
@Transactional
public List<E> findAll() {
return (List<E>) sessionFactory.getCurrentSession().createCriteria(entity).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
}
任何人都可以看到所有这些实施的错误?
答案 0 :(得分:1)
检查此行
String query = "from "+nome_classe+" where "+field+" = :data";
q.setParameter("data", value);
您将value
作为String传递,当hibernate构造一个查询时,它会用String替换一个参数。我认为对于category.Id你使用int类型。
逻辑如下:
它希望Id为int而不是它得到String,然后你得到一个例外。
UPD:
如果您不希望更改实现,请使用setParameter(String name, Object val, Type type)
正确设置列值。
要使用sessionFactory.getClassMetadata(Class.class).getPropertyType("filedName")
答案 1 :(得分:-1)
然后我通过为方法findByField实现两个变体来解决这个问题,一个接受一个String值,另一个接受一个Integer。这种方法的最终代码是:
@SuppressWarnings("unchecked")
@Transactional
public List<E> findByStringField(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";
List<E> instance = sessionFactory.getCurrentSession().createQuery(query).setString("data", value).list();
return instance;
}
@SuppressWarnings("unchecked")
@Transactional
public List<E> findByIntegerField(String field, Integer 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";
List<E> instance = sessionFactory.getCurrentSession().createQuery(query).setInteger("data", value).list();
return instance;
}