2

我想用 spring 和 hibernate 开发一个小型 GWT 应用程序,但是在从 MySql db 获取列表时出现错误。实体类 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;

...二传手和吸气剂

@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;

...二传手和吸气剂

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

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

这是一个例外:-

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)

任何帮助或建议将不胜感激。

亲切的问候,阿比德

4

1 回答 1

0

对我来说,当我使用@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();
于 2012-04-24T10:59:12.000 回答