0

我正在使用 datanucleus 3.2.5 / JDO 将对象持久化到 MongoDB 数据库。

在尝试保留一张列表地图时,我遇到了以下异常:

RuntimeException: json can't serialize type [list element type here]

一些示例代码:

@PersistenceCapable
public class SomeClass {
    private Map<String, List<SomeOtherClass>> myAttribute;
    // ...
}


@PersistenceCapable(embeddedOnly="true")
public class SomeOtherClass {
    private String attribute;
    // ...
}

我可以解决这个问题,将嵌入属性注释为@Serialized,但我更喜欢更优雅的方式。

我错过了什么吗?有没有更好的方法来解决这个问题?

4

1 回答 1

0

引用安迪在 DataNucleus 论坛中对我的问题的回答:

没有持久性规范定义对容器的容器的任何支持。始终建议您将内部容器(在您的情况下为 List)作为中间类的字段。

所以这里有两种方法:

使用中间类

迄今为止最优雅和可维护的解决方案。按照示例玩具代码:

@PersistenceCapable
public class SomeClass {
    private Map<String, SomeOtherClassContainer> myAttribute;
    // ...
}

@PersistenceCapable(embeddedOnly="true")
public class SomeClassContainer {
    private List<SomeOtherClass> myAttribute;
    // ...
}

@PersistenceCapable(embeddedOnly="true")
public class SomeOtherClass {
    private String attribute;
    // ...
}

将属性标记为@Serializable

丑陋,可能是令人头疼的根源,特别是如果依赖于java 默认序列化

@PersistenceCapable
public class SomeClass {
    @Serializable
    private Map<String, List<SomeOtherClass>> myAttribute;
    // ...
}

@PersistenceCapable(embeddedOnly="true")
public class SomeOtherClass implements Serializable {
    private String attribute;
    // ...
}
于 2013-07-30T10:15:52.753 回答