JsonMappingException:无法初始化代理 - 没有通过引用链的Session

时间:2014-10-03 10:24:35

标签: java spring hibernate jackson

我使用以下依赖项,并且我还注册了Hibernate4Module,虽然我为延迟加载实体获取了延迟初始化异常

  Spring :  4.1
  Hibernate : 4.3
  jackson-datatype-hibernate4 : 2.4.2
  jackson-databind : 2.4.2

我的ObjectMapper子类

public class HibernateAwareObjectMapper extends ObjectMapper {
    public HibernateAwareObjectMapper() {
        registerModule(new Hibernate4Module());
     }
    }

和我在应用程序上下文文件中的配置

   <mvc:annotation-driven>
      <mvc:message-converters>
          <!-- Use the HibernateAware mapper instead of the default -->
          <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
              <property name="objectMapper">
                  <bean class="utils.HibernateAwareObjectMapper" />
              </property>
          </bean>
      </mvc:message-converters>
  </mvc:annotation-driven>



public class ProductUsageViewModel {
 private ProductUsage productUsage;

 public String getCollectionNumber() {
   return productUsage.getProduct().getCollectionNumber();
 }


// some other methods 

}

@Entity
@Audited
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class Product implements ModificationTracker {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(nullable = false)
  private Long id;

  // A product may not have a corresponding sample. Some products may be
  // imported from another location. In such a case the corresponding collection
  // field is allowed to be null.
  @CollectedSampleExists
  @ManyToOne(optional=true, fetch=FetchType.LAZY)
  private CollectedSample collectedSample;

  public String getCollectionNumber() {
    if (collectedSample == null)
      return null;
    return collectedSample.getCollectionNumber();
  }

 }






 @Entity
    public class CollectedSample implements ModificationTracker, Comparable<CollectedSample> {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(nullable=false)
  private Long id;


  @Column(length=20, unique=true)
  @Index(name="collectedSample_collectionNumber_index")
  private String collectionNumber;

  public String getCollectionNumber() {
    return collectionNumber;
  }


}

堆栈跟踪

org.springframework.http.converter.HttpMessageNotWritableException:无法写入内容:无法初始化代理 - 无会话(通过参考链:java.util.HashMap [&#34; usage&#34;] - &gt; viewmodel。 ProductUsageViewModel [&#34; collectionNumber&#34;]);嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:无法初始化代理 - 没有Session(通过引用链:java.util.HashMap [&#34; usage&#34;] - &gt; viewmodel.ProductUsageViewModel [&#34] ; collectionNumber&#34;])     在org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:238)     在org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:208)     在org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:153)     在org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:138)     在org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:71)     在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)     在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)     在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)     在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)     在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)     在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)     在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)     在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:646)     在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:727)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)     在org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)     在org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)     在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)     在org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)     在org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)     在org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在CORSFilter.doFilter(CORSFilter.java:25)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)     在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)     在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)     在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)     在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)     在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)     在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)     at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:607)     在org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:313)     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)     在java.lang.Thread.run(未知来源) 引起:com.fasterxml.jackson.databind.JsonMappingException:无法初始化代理 - 没有Session(通过引用链:java.util.HashMap [&#34; usage&#34;] - &gt; viewmodel.ProductUsageViewModel [&#34] ; collectionNumber&#34;])

任何人都可以帮我摆脱那个例外,请...

0 个答案:

没有答案