Spring的Hibernate Proxy对象序列化问题

时间:2014-09-11 07:56:53

标签: java spring hibernate

我正在研究hibernate,并在实体对象中序列化hibernate代理对象时遇到了一个问题。

在一些SO帖子中提到使用Hibernate.initialize(ProxyObject)会起作用, 但是如果我有项目列表,那么我必须遍历所有对象并显式调用这些对象的initialize()方法。

还有其他办法可以解决这个问题吗?

我的实体如下 enter image description here

实体类如下所示

public class GroupMember extends Form {

    /**
     * 
     */
    private static final long serialVersionUID = -4728285634057416148L;

    private int grpmid;         
    private User    user;       
    private Boolean grpownr;    
    private Group   grp;        

    //Other Getter and setters

      @OneToOne(targetEntity = Group.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "Group_GroupId", referencedColumnName = "GroupId")
    public Group getGrp() {
        return grp;
    }
    public void setGrp(Group grp) {
        this.grp = grp;
    }

}

我从服务方法返回时遇到的异常 - 我正在检查休息控制台。

    <html><head><title>Apache Tomcat/7.0.34 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - Could not write JSON: could not initialize proxy - no Session (through reference chain: com.service.responsepojo.GroupMemberResponse[&quot;grpmbr&quot;]-&gt;com.service.pojo.GroupMember[&quot;user&quot;]-&gt;com.service.pojo.User[&quot;dept&quot;]-&gt;com.service.pojo.Department_$$_javassist_9[&quot;un&quot;]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy - no Session (through reference chain: com.service.responsepojo.GroupMemberResponse[&quot;grpmbr&quot;]-&gt;com.service.pojo.GroupMember[&quot;user&quot;]-&gt;com.service.pojo.User[&quot;dept&quot;]-&gt;com.service.pojo.Department_$$_javassist_9[&quot;un&quot;])</h1><HR size="1" noshade="noshade"><p><b>type</b> Exception report</p><p><b>message</b> <u>Could not write JSON: could not initialize proxy - no Session (through reference chain: com.service.responsepojo.GroupMemberResponse[&quot;grpmbr&quot;]-&gt;com.service.pojo.GroupMember[&quot;user&quot;]-&gt;com.service.pojo.User[&quot;dept&quot;]-&gt;com.service.pojo.Department_$$_javassist_9[&quot;un&quot;]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy - no Session (through reference chain: com.service.responsepojo.GroupMemberResponse[&quot;grpmbr&quot;]-&gt;com.service.pojo.GroupMember[&quot;user&quot;]-&gt;com.service.pojo.User[&quot;dept&quot;]-&gt;com.service.pojo.Department_$$_javassist_9[&quot;un&quot;])</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u></p><p><b>exception</b> <pre>org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: could not initialize proxy - no Session (through reference chain: com.service.responsepojo.GroupMemberResponse[&quot;grpmbr&quot;]-&gt;com.service.pojo.GroupMember[&quot;user&quot;]-&gt;com.service.pojo.User[&quot;dept&quot;]-&gt;com.service.pojo.Department_$$_javassist_9[&quot;un&quot;]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy - no Session (through reference chain: com.service.responsepojo.GroupMemberResponse[&quot;grpmbr&quot;]-&gt;com.service.pojo.GroupMember[&quot;user&quot;]-&gt;com.service.pojo.User[&quot;dept&quot;]-&gt;com.service.pojo.Department_$$_javassist_9[&quot;un&quot;])
    2.      org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal(MappingJackson2HttpMessageConverter.java:196)
    3.      org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:179)
    4.      org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:148)
    5.      org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:90)
    6.      org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:189)
    7.      org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:69)
    8.      org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)
    9.      org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
    10.     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
    11.     org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    12.     org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    13.     org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    14.     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    15.     org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:822)
    16.     javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    17.     org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    18.     javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    19. </pre></p><p><b>root cause</b> <pre>com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy - no Session (through reference chain: com.service.responsepojo.GroupMemberResponse[&quot;grpmbr&quot;]-&gt;com.service.pojo.GroupMember[&quot;user&quot;]-&gt;com.service.pojo.User[&quot;dept&quot;]-&gt;com.service.pojo.Department_$$_javassist_9[&quot;un&quot;])
    20.     com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:232)
    21.     com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:197)
    22.     com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:183)
    23.     com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:559)
    24.     com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:143)
    25.     com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:544)
    26.     com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:551)
    27.     com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:143)
    28.     com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:544)
    29.     com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:551)
    30.     com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:143)
    31.     com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:544)
    32.     com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:551)
    33.     com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:143)
    34.     com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:120)
    35.     com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:1728)
    36.     org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal(MappingJackson2HttpMessageConverter.java:193)
    37.     org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:179)
    38.     org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:148)
    39.     org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:90)
    40.     org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:189)
    41.     org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:69)
    42.     org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)
    43.     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
    44.     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
    45.     org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    46.     org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    47.     org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    48.     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    49.     org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:822)
    50.     javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    51.     org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    52.     javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    53. </pre></p><p><b>root cause</b> <pre>org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    54.     org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:86)
    55.     org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140)
    56.     org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
    57.     com.service.pojo.Department_$$_javassist_9.getUn(Department_$$_javassist_9.java)
    58.     sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    59.     sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    60.     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    61.     java.lang.reflect.Method.invoke(Method.java:601)
    62.     com.fasterxml.jackson.databind.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:653)
    63.     com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:509)
    64.     com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:551)
    65.     com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:143)
    66.     com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:544)
    67.     com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:551)
    68.     com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:143)
    69.     com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:544)
    70.     com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:551)
    71.     com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:143)
    72.     com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:544)
    73.     com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:551)
    74.     com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:143)
    75.     com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:120)
    76.     com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:1728)
    77.     org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal(MappingJackson2HttpMessageConverter.java:193)
    78.     org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:179)
    79.     org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:148)
    80.     org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:90)
    81.     org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:189)
    82.     org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:69)
    83.     org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)
    84.     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
    85.     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
    86.     org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    87.     org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    88.     org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    89.     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    90.     org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:822)
    91.     javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    92.     org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    93.     javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    94. </pre></p><p><b>note</b> <u>The full stack trace of the root cause is available in the Apache Tomcat/7.0.34 logs.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.34</h3></body></html>

更新 - 目前我正在使用一个逻辑来使对象unproxy并将其返回给客户端,但这很昂贵,因为我必须遍历大量的项目列表

Criterion classCriterion = Restrictions.eq(“U.un",
        requestForm.getUn());

Criteria criteria = sessionObject
        .createCriteria(GroupMember.class, "GPM")
        .createAlias("GPM.user”, “U”)
        .add(classCriterion);

@SuppressWarnings("unchecked")
List<GroupMember> members = (List<GroupMember>)criteria.list();
for (GroupMember groupMember : members) {
    if (groupMember.getGrp() instanceof HibernateProxy) {
        groupMember.setGrp((Group) UnProxy
                .initializeAndUnproxy(groupMember.getGrp()));
    }

    if (groupMember.getGrp().getDept() instanceof HibernateProxy) {
        groupMember.getGrp().setDept((Department) UnProxy
                .initializeAndUnproxy(groupMember.getGrp().getDept()));
    }

    if (groupMember.getUser().getDept() instanceof HibernateProxy) {
        groupMember.getUser().setDept((Department) UnProxy
                .initializeAndUnproxy(groupMember.getUser().getDept()));
    }
}

0 个答案:

没有答案