Как повторно прикрепить объект к сеансу EclipseLink после десериализации

StackOverflow https://stackoverflow.com/questions/3699345

Вопрос

Вот простой 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, чтобы получить список ленивости комнаты.

Это было полезно?

Решение

Присоединиться к объекту, вы должны использовать EntityManager.merge метод до доступа к его отношениям.

Что-то вроде:

hotel2 = em.merge(hotel2);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top