mysql - hibernate ManyToOne 关系对象中的 GWT rpc.SerializationException

标签 mysql spring hibernate gwt annotations

我想用 spring 和 hibernate 开发一个小型 GWT 应用程序,但在从 MySql 数据库获取列表时遇到错误。实体类 Contact 与 Room 有@ManyToOne 关系。

@Entity
@Table(name = "contact")

public **class Contact** extends LightEntity implements java.io.Serializable {

@Id
@Column(name = "id")
protected int id;   

@Column(name = "firstname")
protected String firstname;   

@Column(name = "telephone")    
protected String telephone;

@ManyToOne
@JoinColumn(name = "ROOM_ID")  
protected Room room;

...setter 和 getter

@Entity
@Table(name = "ROOM")

public **class Room** extends LightEntity implements java.io.Serializable {

@Id
@GeneratedValue
@Column(name = "ROOM_ID")
private int id;     

@Column(name = "ROOM_NUMBER")    
protected int rnr;

@OneToMany(mappedBy="room", fetch = FetchType.EAGER) // tried to fetch eagerly
private List<Contact> contacts;

...setter 和 getter

LightEntity 已按照文章 https://developers.google.com/web-toolkit/articles/using_gwt_with_hibernate?hl=fr-FR 中的说明使用

当我仅使用单向(即使用@ManyToOne 联系房间)关系时,它工作正常

这里是异常(exception):-

Hibernate: select contacts0_.ROOM_ID as ROOM6_1_1_, contacts0_.id as id1_, contacts0_.id as id0_0_, contacts0_.email as email0_0_, contacts0_.firstname as firstname0_0_, contacts0_.lastname as lastname0_0_, contacts0_.ROOM_ID as ROOM6_0_0_,  contacts0_.telephone as telephone0_0_ from contact contacts0_ where contacts0_.ROOM_ID=?

Starting Jetty on port 8888
   [WARN] Exception while dispatching incoming RPC call
com.google.gwt.user.client.rpc.SerializationException: 
Type 'org.hibernate.collection.PersistentBag' 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 = [com.project.server.schema.Contact@168e461, com.project.server.schema.Contact@dde1f6,  com.project.server.schema.Contact@c66a3b, com.project.server.schema.Contact@8103d8, com.project.server.schema.Contact@137dfac]
    at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serialize(ServerSerializationStreamWriter.java:619)

如有任何帮助或建议,我们将不胜感激。

亲切的问候, 阿比德

最佳答案

对我来说,当我使用@LazyCollection 时,这个问题就消失了:

@LazyCollection(LazyCollectionOption.FALSE)

完整示例:

@ManyToOne(cascade = CascadeType.MERGE, optional=false)
@LazyCollection(LazyCollectionOption.FALSE)
@JoinColumn(name="room_id")
public Room getRoom();

@OneToMany(mappedBy = "contact")
@LazyCollection(LazyCollectionOption.FALSE)
public List<Contact> getContacts();

关于mysql - hibernate ManyToOne 关系对象中的 GWT rpc.SerializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10293439/

相关文章:

mysql - MYSQL中针对子查询的查询优化?

导入时 MySQL LOAD XML 缺少子节点

mysql - 列出具有特定列名的列值的所有表名

java - Hibernate @ManyToOne 删除一侧的条目,在多侧将 FK 设置为 NULL

java - JPA @EntityListeners、@PrePersist 和 Spring @RepositoryEventHandler、@HandleBeforeSave 之间的区别

php - 如何正确显示存储在 MySQL 数据库中的不可读字符

spring - javax.servlet.jsp.JspTagException : No message found under code 'errors.contact.sysadmin.msg' for locale 'en_US' .]

java - Tomcat如何从context.xml加载资源

java - JPA - 使用中间表在同一表之间映射 OneToMany 关联

java - hibernate @ManyToOne 无法正确插入,因为 FK id 尚未创建