【发布时间】:2015-11-29 03:30:20
【问题描述】:
我遇到了错误:
警告:无法序列化会话属性 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 的代码
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;
}
}
当然,还有更多代码,但我认为这是重要的部分。
我猜 NativeDockindQuery 必须是可序列化的,因为它是 DocumentFieldHelper 方法之一中的返回类型?
我是否有可能因为我使用的是 DocumentKind 的静态方法而遇到这个问题?
【问题讨论】:
-
fieldValues里面有什么? -
我看到两个 documentKind 。一个是 DocumentFieldHelper 中的字符串变量,另一个看起来像 NativeDockindQuery 中的类类型。这样可以吗?我的意思是你在这里弄错了什么?
-
Vlad - fieldValues 对象变量是原语,如 int,long 也是 String。没有什么是无法序列化的(尤其是错误描述中指出的 DocumentKind)
-
SacJn - 不,一切都很好。在 DocumentFieldHelper 中,documentKind 是字符串。在 NativeDockindQuery 中创建时转换为 DocumentKind 类实例。
标签: java serialization transient