i have 3 classes, Recommendation, Judgement, User, the relationship are: one recommendation might have multiple judgements, one user might have multiple judgements(on different recommendation), here are my JPA classes:
Class Recommendation
@Entity
@Table(name = "RECOMMENDATIONS")
public class Recommendation implements Serializable, Cloneable {
...
@OneToMany(cascade = {CascadeType.REFRESH, CascadeType.DETACH }, fetch = FetchType.EAGER, mappedBy="recommendation", targetEntity=Judgement.class)
private Set<Judgement> judgements;
...
public Recommendation() {
super();
this.judgements = new LinkedHashSet<Judgement>();
}
...
}
class User:
@Table(name = "USERS", uniqueConstraints = @UniqueConstraint(columnNames = "email"))
public class User implements Serializable, Cloneable {
...
@OneToMany(mappedBy="judge", targetEntity=Judgement.class, cascade = { CascadeType.REFRESH, CascadeType.DETACH }, fetch = FetchType.EAGER)
private Set<Judgement> judgements;
...
public User() {
super();
this.judgements = new LinkedHashSet<Judgement>();
}
...
}
class Judgement:
@Entity
@Table(name = "JUDGEMENTS")
public class Judgement implements Serializable, Cloneable {
@ManyToOne(cascade = { CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(name = "id")
private Recommendation recommendation;
@ManyToOne(cascade = { CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(name = "user_id")
private User judge;
...
public Judgement() {
super();
}
...
}
in db, i have the following tables:
users
recommendations;
judgements;
i have some data in users, and some in recommendations, but empty in judgements table, when i loaded my application got the following exception:
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.: instance = []
i debugged the code turned out when load the judgements field for the recommendation record, it cannot instantiate it. Please help!
Objects containing database entities, which are loaded by hibernate, are not serializable. As a consequence they can’t be used as transfer objects for gwt remote procedure calls.
The simplest solution is to copy all content from the entity instances (in your case instances of Recommendation, Judgement and User) into simple data holder objects and use the holder objects as parameters in the remote procedure calls. For small projects that is a good solution. For bigger projects you can consider of using the Gilead library; in that case you even can profit of lazy loading.
Google provides a good documentation of the integration of hibernate into GWT.