Serializable Hibernate data object for GWT RPC

你离开我真会死。 提交于 2020-01-25 04:51:10

问题


I have a simple POJO mapped to a table using Hibernate. Which works just fine.

public class Patient implements Serializable {

    private int    patientId;
    private String firstName;
    private String lastName;
    private Set<Prescription> patientPrescriptions;

    public Patient() {}

    ...
}

My problem is I want to be able to serialize the object so I can get it trough the wire for my GWT-RPC calls. If my async service return this object I get an error:

com.google.gwt.user.client.rpc.SerializationException: Type 'org.hibernate.collection.PersistentSet' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.

I suppose this is due to the fact it cannot serialize Set since this is an interface hence not serializable. On the other hand Hibernate needs a collection interface (Set/Map) to work. So this means I can no longer send objects mapped with Hibernate? Is there some "easy" way to serialize a Set?

Thank you.


回答1:


The problem is that GWT does not find the source code of the annotations and the used classes. GWT needs that source code since it has to compile your object into JS.

I want to be able to serialize the object so I can get it trough the wire for my GWT-RPC calls.

You can use Gilead to do that. (Previously known as Hibernate4GWT)

I suppose this is due to the fact it cannot serialize Set since this is an interface hence not serializable. On the other hand Hibernate needs a collection interface (Set/Map) to work. So this means I can no longer send objects mapped with Hibernate? Is there some "easy" way to serialize a Set?

The problem is not with Set, GWT can very well serialize collections through its GWT-RPC. Although during the transmission you will want to use a specific implementation like HashSet to allow it to optimize for that particular implementation, instead of a generic interface.




回答2:


Just to add details on how I overcame this Serialization problem:

1- I have 2 data model objects (which is awful but I ain't got time for a school project to clean this up).

// The persistent data model class
public class PatientPersistent implements Serializable {
    ...

    Set<Prescription> patientPrescriptions;

    ...
}

// The serializable over the wire (GWT-RPC) data model class
public class Patient implements Serializable {
    ...

    Set<Prescription> patientPrescriptions;

    ...
}

2- I use Dozer to map persistant-model <== to the ==> Serializable data class Patient. Like this:

Patient thePatient = mapper.map(persistentObject, Patient.class);

This Patient is sent back by my Async service to the client. After this mapping you can see that the Patient has a HashSet<> type from java.util instead of the hibernate non serializable version.



来源:https://stackoverflow.com/questions/3282753/serializable-hibernate-data-object-for-gwt-rpc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!