HiberObjects

Download

Order

Forum

Vote for this plugin

Serialization for GWT

HiberObjects can generate Data Transfer Objects (DTO's) that can be used to serialize Hibernate or JPA persisted objects to a GWT client.

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

Creating a GWT Project

It's easiest to create a project from scratch using the GWT scripts.

To add GWT to an existing project, you may do as follows:

Data Transfer Objects

<<Persistent>> classes cannot be used in a GWT 1.4 client because GWT does not support annotations and generics. Furthermore, to transfer objects in RPC calls, they need to implement IsSerializable.

HiberObjects can generate Data Transfer Objects (DTO's) for this purpose. The DTO classes will be similar to the <<Persistent>> classes, with the following changes:

To generate DTO's, open Project Properties, go to the Persistence page, and select Generate DTO's for GWT.

Dozer

To convert between <<Persistent>> objects and DTO's, a tool called Dozer can be used.

To use Dozer, please 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.

Here is a small example of a GWT servlet that uses JPA to access a database and Dozer to convert <<Persistent>> objects to DTO's for serialization to the GWT client:

public class FlightServiceImpl extends RemoteServiceServlet implements FlightService { private HibernateHelper persistenceHelper = HibernateHelper.getInstance(); private FlightDAO flightDAO = new FlightDAO(); private MapperIF mapper = new DozerBeanMapper(); public FlightServiceImpl() { // Create some initial objects in the database for testing: EntityManager entityManager = persistenceHelper.getEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); FlightPersistentTest initFlights = new FlightPersistentTest(); initFlights.init(); tx.commit(); } public List listFlights() { EntityManager entityManager = persistenceHelper.getEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); try { List<Flight> flights = flightDAO.list(); ArrayList<FlightDTO> list = new ArrayList<FlightDTO>(); for(Flight f : flights) { FlightDTO dto = (FlightDTO) mapper.map(f, FlightDTO.class); list.add(dto); } tx.commit(); return list; } catch(RuntimeException e) { tx.rollback(); throw e; } } }