Java getDeclaringClass导致延迟初始化异常失败
我需要在GSON中使用我自己的排除策略,因此我创建了这个类:Java getDeclaringClass导致延迟初始化异常失败,java,spring,hibernate,spring-mvc,annotations,Java,Spring,Hibernate,Spring Mvc,Annotations,我需要在GSON中使用我自己的排除策略,因此我创建了这个类: public class DUExclusionStrategy implements ExclusionStrategy { public boolean shouldSkipField(FieldAttributes f) { Collection<Annotation> annots = f.getAnnotations(); for(Annotation a : annots
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;
}
}
@OneToMany(mappedBy="parent", fetch=FetchType.LAZY) @OrderBy("ordinal")
private Set<T> children = new TreeSet<T>();
我能用简单优雅的方式解决这个问题吗?我真的不明白为什么getDeclaringClass()
会导致BaseEntityImpl的完全非序列化
提前谢谢 在
BaseEntityImpl
实体更改fetchType
懒惰到渴望,即
@OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
我写道:“我不应该改变BaseEntityImpl中的任何内容。”。这不是解决方案,但谢谢。:-)这是您需要以另一种方式急切获取的方法,使方法在服务中具有事务性并初始化对象。您确定
getDeclaringClass()
是问题所在,没有它也可以工作吗?stacktrace中没有提到它。您在DUSerializer#serialize()
中做什么?它在事务内部吗?是的,我确定。没有这个调用(或调用f.getName()),一切都可以正常工作。我曾尝试将方法用于事务,但没有解决任何问题。。
@OneToMany(mappedBy="parent", fetch=FetchType.EAGER)