Q:

GWT exception "Cannot Serialize" when using Hibernate

I'm getting "com.google.gwt.user.client.rpc. SerializationException:", when employing GWT+Hibernate.  I tried to Google, but most of the error is in the context of List (I found a similiar thread here GWT Cannot Serialize My Object from Hibernate).  I'm getting this when I'm trying to pass an object of type UserPreference through RPC.  Below are the classes for your reference. 
public class UserPreference implements Serializable {

private static final long serialVersionUID = 1L;
private UserInfo userInfo;

// other attributes

public UserPreference() {
    super();
}
    // getters/setters
}

// UserInfo Class
public class UserInfo implements Serializable{

private int id;
private String name;
private String cuid;    
private UserRole role;
private DbSchema favDb;

public UserInfo() {
    super();
}
}

//DbSchema class
public class DbSchema implements Serializable{

private static final long serialVersionUID = 1L;

private int id;
private String name;
private int port;
private String host;
private String sidName;
private String userName;
private String password;
private String defaultConfigId;

public DbSchema() {
    super();
}

    //getters/setters
}

I don't know what the problem is with my 'DbSchema' class.  All the data is being retrieved fine from the DB, and when I make DbSchema instance as 'transient' in UserInfo class, this is working fine.  I didn't try with Dozer or Gilead, but just want to know the actual issue with the code. 
Thanks for the help. 

A:

You have a problem because Hibernate is lazy fetching referenced objects: when UserInfo object is created by Hibernate, favDb is not pointing to an actual DbSchema object but to a Hibernate-generated proxy (this is actually implemented via getter/setter not the field itself). 
A general solution is to create a DTO (data transfer object).  See this blogpost for explanation and a solution: http://hibernate4gwt.sourceforge.net/hibernate_gwt_lazy_issue.html

