反序列化后如何将对象重新附加到 EclipseLink 会话

发布于 2024-09-19 09:12:03 字数 2257 浏览 5 评论 0原文

这是一个简单的 POC:

public void main(String[] args) {
    final String FILE_NAME = "c:/poc.ser";
    try {
        HotelJdo hotel = HotelJdoFinder.findById(430);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME));
        // serialize the object
        oos.writeObject(hotel);
        oos.close();
        // read the object in the same vm
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME));
        HotelJdo hotel2 = (HotelJdo) ois.readObject();
        // this line throws an exception
        System.out.println("number of crs channels: " + hotel2.getAvailableRooms().size());
        ois.close();
    } catch (Exception e) {
        System.out.println("Unexpected exception:");
        e.printStackTrace();
    }
}

hotel2.getAvailableRooms() 查询配置为使用透明间接的房间列表。该调用引发以下异常:

Exception [EclipseLink-7242] (Eclipse Persistence Services - 1.2.1.v20100428-r70
82): org.eclipse.persistence.exceptions.ValidationException
Exception Description: An attempt was made to traverse a relationship using indi
rection that had a null Session.  This often occurs when an entity with an unins
tantiated LAZY relationship is serialized and that lazy relationship is traverse
d after serialization.  To avoid this issue, instantiate the LAZY relationship p
rior to serialization.
        at org.eclipse.persistence.exceptions.ValidationException.instantiatingV
alueholderWithNullSession(ValidationException.java:975)
        at org.eclipse.persistence.internal.indirection.QueryBasedValueHolder.in
stantiate(QueryBasedValueHolder.java:83)
        at org.eclipse.persistence.internal.indirection.QueryBasedValueHolder.in
stantiate(QueryBasedValueHolder.java:75)
        at org.eclipse.persistence.internal.indirection.DatabaseValueHolder.getV
alue(DatabaseValueHolder.java:83)
        at org.eclipse.persistence.indirection.IndirectList.buildDelegate(Indire
ctList.java:237)
        at org.eclipse.persistence.indirection.IndirectList.getDelegate(Indirect
List.java:397)
        at org.eclipse.persistence.indirection.IndirectList.size(IndirectList.ja
va:726)

我知道我可以在序列化之前触发房间列表的初始化,但我想知道如何将 hotel2 对象重新附加到 EclipseLink 会话,以便延迟获取房间列表。

Here's a simple POC:

public void main(String[] args) {
    final String FILE_NAME = "c:/poc.ser";
    try {
        HotelJdo hotel = HotelJdoFinder.findById(430);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME));
        // serialize the object
        oos.writeObject(hotel);
        oos.close();
        // read the object in the same vm
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME));
        HotelJdo hotel2 = (HotelJdo) ois.readObject();
        // this line throws an exception
        System.out.println("number of crs channels: " + hotel2.getAvailableRooms().size());
        ois.close();
    } catch (Exception e) {
        System.out.println("Unexpected exception:");
        e.printStackTrace();
    }
}

hotel2.getAvailableRooms() queries list of rooms that configured to use transparent indirection. And that call throws the following exception:

Exception [EclipseLink-7242] (Eclipse Persistence Services - 1.2.1.v20100428-r70
82): org.eclipse.persistence.exceptions.ValidationException
Exception Description: An attempt was made to traverse a relationship using indi
rection that had a null Session.  This often occurs when an entity with an unins
tantiated LAZY relationship is serialized and that lazy relationship is traverse
d after serialization.  To avoid this issue, instantiate the LAZY relationship p
rior to serialization.
        at org.eclipse.persistence.exceptions.ValidationException.instantiatingV
alueholderWithNullSession(ValidationException.java:975)
        at org.eclipse.persistence.internal.indirection.QueryBasedValueHolder.in
stantiate(QueryBasedValueHolder.java:83)
        at org.eclipse.persistence.internal.indirection.QueryBasedValueHolder.in
stantiate(QueryBasedValueHolder.java:75)
        at org.eclipse.persistence.internal.indirection.DatabaseValueHolder.getV
alue(DatabaseValueHolder.java:83)
        at org.eclipse.persistence.indirection.IndirectList.buildDelegate(Indire
ctList.java:237)
        at org.eclipse.persistence.indirection.IndirectList.getDelegate(Indirect
List.java:397)
        at org.eclipse.persistence.indirection.IndirectList.size(IndirectList.ja
va:726)

I understand that I can just trigger initialization of room list before serialization but I wonder how can I re-attach hotel2 object to EclipseLink session in order to get the list of room lazily.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷爱 2024-09-26 09:12:10

要重新附加对象,您必须在访问其关系之前使用 EntityManager.merge 方法。

像这样的东西:

hotel2 = em.merge(hotel2);

To reattach an object, you have to use the EntityManager.merge method before accessing its relationships.

Something like:

hotel2 = em.merge(hotel2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文