Java NotSerializableException,即使在使用瞬态
我收到错误:Java NotSerializableException,即使在使用瞬态,java,serialization,transient,Java,Serialization,Transient,我收到错误: 警告:无法序列化的会话属性DocumentFieldHelper 会话{idsession}java.io.NotSerializableException: com.example.DocumentKind尝试序列化类时 DocumentFieldHelper DocumentFieldHelper的代码 private class DocumentFieldHelper implements Serializable { private
警告:无法序列化的会话属性DocumentFieldHelper 会话{idsession}java.io.NotSerializableException: com.example.DocumentKind
尝试序列化类时 DocumentFieldHelper DocumentFieldHelper的代码
private class DocumentFieldHelper implements Serializable
{
private static final long serialVersionUID = 1L;
private Map<String, Object> fieldValues;
private String documentKind;
public DocumentFieldHelper()
{
fieldValues = new HashMap<String, Object>();
}
public NativeDockindQuery createQuery()
{
try
{
NativeDockindQuery ndq = NativeDockindQuery.create(this.getDocumentKind());
return ndq;
} catch (EdmException e)
{
log.error(e.getMessage(), e);
}
return null;
}
public String getDocumentKind() {
return documentKind;
}
当然,还有更多的代码,但我认为这是重要的部分
我猜NativeDockindQuery必须是可序列化的,因为它是DocumentFieldHelper方法之一中的返回类型
有没有可能因为我使用DocumentKind中的静态方法而出现这个问题?好的,我已经做了一些测试,当然,除了我发布的代码之外,其他地方也有问题。我应该提到的是,这段代码不是我的,我只是在看到一些bug后进行更正。 事实证明,DocumentFieldHelper类是其他类中的内部类。外部类声明了DocumentKind类的实例变量,这就是问题出现的原因。我刚从外部类创建了DocumentFieldHelper独立类,一切都很好。 课程序列化内部类时要小心
感谢Vlad和SacJn的回复。好的,我已经做了一些测试,当然,除了我发布的代码之外,还有其他地方出现了问题。我应该提到的是,这段代码不是我的,我只是在看到一些bug后进行更正。 事实证明,DocumentFieldHelper类是其他类中的内部类。外部类声明了DocumentKind类的实例变量,这就是问题出现的原因。我刚从外部类创建了DocumentFieldHelper独立类,一切都很好。 课程序列化内部类时要小心
感谢Vlad和SacJn的回复。字段值
中包含了什么?我看到了两种文档。一个是DocumentFieldHelper中的字符串变量,另一个看起来像NativeDockindQuery中的类类型。这样行吗?我的意思是你搞错了吗?Vlad-fieldValues对象变量是像int,long和String这样的基本体。没有什么是不能序列化的(特别是像错误描述中指出的DocumentKind)SacJn-不,一切都很好。在DocumentFieldHelper中,documentKind是字符串。在NativeDockInQuery中创建时,它被转换为DocumentKind类实例。一个是DocumentFieldHelper中的字符串变量,另一个看起来像NativeDockindQuery中的类类型。这样行吗?我的意思是你搞错了吗?Vlad-fieldValues对象变量是像int,long和String这样的基本体。没有什么是不能序列化的(特别是像错误描述中指出的DocumentKind)SacJn-不,一切都很好。在DocumentFieldHelper中,documentKind是字符串。它在NativeDockInQuery中创建时转换为DocumentKind类实例。
public class NativeDockindQuery implements Serializable {
private static final long serialVersionUID = -2001430456575525419L;
private transient DocumentKind kind;
public static NativeDockindQuery create(String kind) throws EdmException {
return new NativeDockindQuery(DocumentKind.findByCn(kind), false);
}
private NativeDockindQuery(DocumentKind kind, boolean checkPermissions) throws EdmException {
this.kind = kind;
}
}