Denys.Kristov
Denys.Kristov

Reputation: 83

GWT RPC SerializationException while using List of Entities @OneToMany

I have 2 serializable with zero-constructor Entities, with @OneToMany relation (if i use just one entity without @OneToMany List, everything work right):

@Entity
@Table(name = "directory")
public class Directory implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String documentName;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<DirectoryValues> dirValues;

    public Directory() {}
    ...
    getters and setters
    }


@Entity
@Table(name = "directoryvalues")
public class DirectoryValues implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String value;

    public DirectoryValues() {}
    ...
    getters and setters

}

The problem appears when i add @OneToMany annotation:

SEVERE: 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 = [] at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serialize(ServerSerializationStreamWriter.java:619)

What is wrong with my List in entity?

Upvotes: 1

Views: 1170

Answers (2)

Geoffrey Wiseman
Geoffrey Wiseman

Reputation: 5637

As philfr49 implied, GWT and Hibernate have some troubles working together at times. It's been well-documented and you can read about some of the alternatives in the links he sent. You can either avoid GWT-RPC, avoid Hibernate, or work a little harder to make them compatible.

I have a project where GWT and Hibernate are used together; I use data transfer objects and I translate and update the domain and DTO using Moo. It's a bit of a pain at times, but I built Moo in part to make that job easier. There are other similar frameworks you can use (e.g. Dozer, etc.)

Ultimately, deciding how best to address this is something that you have to work out for your own project.

Upvotes: 0

Philippe Gonday
Philippe Gonday

Reputation: 1766

You must use Data Transfer Objects (DTO), read: http://www.gwtproject.org/articles/using_gwt_with_hibernate.html and especially the "Why Hibernate objects can't be understood when they reach the browser world" part. Or use RequestFactory (http://www.gwtproject.org/doc/latest/DevGuideRequestFactory.html) instead of GWT-RPC.

Upvotes: 2

Related Questions