我有3个班级,推荐,判断,用户,关系是:一个推荐可能有多个判断,一个用户可能有多个判断(关于不同推荐),这里是我的JPA课程:
班级推荐
@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>();
}
...
}
类用户:
@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>();
}
...
}
阶级判断:
@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();
}
...
}
在db中,我有以下表格: 用户 建议; 判断;
我在用户中有一些数据,有些在推荐中,但在判断表中为空,当我加载应用程序时出现以下异常:
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 = []
我调试了在加载推荐记录的判断字段时结果出来的代码,它无法实例化它。请帮忙!
答案 0 :(得分:2)
包含由hibernate加载的数据库实体的对象不可序列化。因此,它们不能用作gwt远程过程调用的传输对象。
最简单的解决方案是将实体实例中的所有内容(在您的建议,判断和用户的情况下)复制到简单的数据持有者对象中,并使用持有者对象作为远程过程调用中的参数。对于小项目来说这是一个很好的解决方案对于更大的项目,您可以考虑使用Gilead库;在这种情况下,你甚至可以从延迟加载中获利。
Google提供了a good documentation将hibernate集成到GWT中。