views:

135

answers:

2

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  A: 

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.

Bakkal
Will try this. Also found this other link which is pretty much the same question. Thx.http://stackoverflow.com/questions/3183269/hibernate-and-serializable-entities
code-gijoe
Thanks, though I used (at least for now) the Dozer solution. I now understand this GWT JRE Emulation Library issue.
code-gijoe
A: 

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

1- I now two 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.

code-gijoe