DB Importer

HiberObjects

Download

Order

Forum

Serialization for GWT

This page describes how to use HiberObjects with GWT 1.5.

If you use GWT 1.4, HiberObjects can generate Data Transfer Objects (DTO's) that can be used to serialize Hibernate or JPA persisted objects to a GWT client. This is described on the Help pages.

Note: This functionality is intended for developers that are already familiar with GWT. Please refer to the GWT documentation for more details.

GWT Services

To design a GWT Service in HiberObjects, set the stereotype of a class to <<GWT Service>>. This will generate code for a GWT servlet and the 2 interfaces that are necessary:

public class LoginImpl extends RemoteServiceServlet implements Login { public Login() { } public void login(String username, String password) { } } public interface Login extends RemoteService { void login(String username, String password); } public interface LoginAsync { void login(String username, String password, AsyncCallback callback); }

Generic parameters are generated for GWT 1.5 or @gwt.typeArgs Javadoc for GWT 1.4. Please configure wich version of GWT you are using in Project Properties, on the HiberObjects > GWT page.

Serialization

GWT 1.5 solved most of the problems with serializing Hibernate and JPA objects. This means you can send your <<Persistent>> objects between a GWT server and client.

There are 3 things you need to do:

  1. Make sure the <<Persistent>> classes are in the GWT client's source path.
  2. Package the Hibernate or JPA JARs in the client deployment.
  3. Fetch all objects in lazy collections. Hibernate and JPA use proxy collections to not fetch objects from the database before needed. These proxy objects cannot be serialized by GWT, so they must be replaced with actual collections before serialization.

GWT Source Path

To make sure the <<Persistent>> classes are in the GWT client's source path, you can either design the classes in a sub-package of client, or you can declare the persistent class package as a source path in the gwt xml file like this:

<source path='client'/> <source path='model'/>

Unpack Lazy Collections

You can use Dozer to replace all proxy collections with actual collections.

Here is an example of how this can be done:

public List<User> listUsers() { EntityManager entityManager = persistenceHelper.getEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); try { List<User> users = userDAO.getAllUsers(); List<User> dtoList = serialize(users); tx.commit(); entityManager.close(); return dtoList; } catch(RuntimeException e) { e.printStackTrace(); tx.rollback(); throw e; } } public <T> List<T> serialize(Collection<T> list) { ArrayList<T> dtoList = new ArrayList<T>(); for(T a : list) { T dto = null; if(a != null) { dto = (T) mapper.map(a, a.getClass()); } dtoList.add(dto); } return dtoList; }

There are other possibilities than Dozer. See for instance hibernate4gwt.

To use Dozer, download and add the following libraries to your project. See details on the Download page.

The following libraries are also necessary, but HiberObjects will add them automatically to your project if Add Hibernate Libraries is selected in the Persistence page of Project Properties.