我需要在GSON中使用自己的ExclusionStrategy,所以我创建了此类:

public class DUExclusionStrategy implements ExclusionStrategy {
    public boolean shouldSkipField(FieldAttributes f) {
        Collection<Annotation> annots = f.getAnnotations();
        for(Annotation a : annots) {
            if(a.annotationType().equals(Expose.class)) {
                return false;
            }
        }

        return (f.getDeclaringClass() == BaseEntityImpl.class && f.getName().equals("creation"))
                || (f.getDeclaringClass() == BaseEntityImpl.class && f.getName().equals("creator"))
                || (f.getDeclaringClass() == BaseEntityImpl.class && f.getName().equals("modification"))
                || (f.getDeclaringClass() == BaseEntityImpl.class && f.getName().equals("modificator"));
    }

    public boolean shouldSkipClass(Class<?> clazz) {
        return false;
    }
}


我不应该在BaseEntityImpl中进行任何更改,并且此类中包含以下字段:

@OneToMany(mappedBy="parent", fetch=FetchType.LAZY) @OrderBy("ordinal")
private Set<T> children = new TreeSet<T>();


而且由于调用f.getDeclaringClass(),应用程序引发了此异常:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: cz.bach.dataframe2.domain.impl.BaseEntityImpl.children, could not initialize proxy - no Session
    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:566)
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
    at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:545)
    at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:124)
    at org.hibernate.collection.internal.PersistentSet.iterator(PersistentSet.java:180)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:95)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
    at com.google.gson.Gson.toJson(Gson.java:593)
    at com.google.gson.Gson.toJson(Gson.java:572)
    at com.google.gson.Gson.toJson(Gson.java:527)
    at com.google.gson.Gson.toJson(Gson.java:507)
    at cz.bach.clients.du.model.service.DUSerializer.serialize(DUSerializer.java:23)
    at cz.bach.clients.du.model.service.DURepository.save(DURepository.java:619)
    at cz.bach.clients.du.model.service.DURepository.appendChild(DURepository.java:176)
    at cz.bach.clients.du.model.service.DURepository.appendChild(DURepository.java:150)
    at cz.bach.clients.du.model.service.DURepository$$FastClassByCGLIB$$f2fe0d5d.invoke(<generated>)
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
    at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
    at cz.bach.clients.du.model.service.DURepository$$EnhancerByCGLIB$$a27cbe9c.appendChild(<generated>)
    at cz.bach.clients.du.ui.perspectives.main.DUDispatcher.add(DUDispatcher.java:261)
    at cz.bach.archives.uiframe.actions.CommonAddAction.run(CommonAddAction.java:11)
    at cz.bach.clients.du.ui.perspectives.main.DUToolbar$1.buttonClick(DUToolbar.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:510)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:164)
    at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1219)
    at com.vaadin.ui.Button.fireClick(Button.java:567)
    at com.vaadin.ui.Button.changeVariables(Button.java:223)
    at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.changeVariables(AbstractCommunicationManager.java:1460)
    at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariableBurst(AbstractCommunicationManager.java:1404)
    at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariables(AbstractCommunicationManager.java:1329)
    at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.java:761)
    at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest(CommunicationManager.java:325)
    at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:501)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:83)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)


我可以通过简单优雅的方式解决此问题吗?我真的不知道为什么getDeclaringClass()应该引起BaseEntityImpl的完全初始化的任何原因。

提前致谢!

最佳答案

BaseEntityImpl实体中,将fetchType懒惰更改为Eager,即

@OneToMany(mappedBy="parent", fetch=FetchType.EAGER)

09-05 10:28