hibernate4gwt
http://hibernate4gwt.sourceforge.net
hosted by
SourceForge.net Logo
| Home | News | Getting started | OverviewHibernate-GWT issues | FAQ | Thanks |

Getting started (Java5 support)

This sections describes the simplest way to enable hibernate4gwt for a Java5 Domain classes in GWT 1.4 application (since GWT 1.5 now supports Java5 syntax natively, please use the stateless, stateful or dynamic proxy mode).

1. Installation

  • Download the hibernate4gwt distribution. All the dependency libraries are included in the "lib" directory
  • Copy all the jar files and put them in a folder or to the root directory of your gwt application, and add the libraries to your classpath. The easiest way to do that if you are using eclipse is to add the jars from the eclipse classpath configuration utility or alternatively you can use an ant script to add the required libraries to your classpath.
2. Configuration
  • Edit your gwt xml file to inherit from hibernate4gwt and declare the domain package
<module>
       <!-- Inherit the core Web Toolkit stuff.          -->
       <inherits name='com.google.gwt.user.User'/>
       <inherits name='net.sf.hibernate4gwt.Hibernate4Gwt14'/>

        <!-- Specify the app entry point class.           -->
        <entry-point class='...'/>

       <!-- Additional source path -->
       <source path='dto'/>
       <source path='client'/>

       <!-- Servlet path -->
       ...
</module>

3. DTO classes
  • Put all your DTO classes in a shared folder between client and server : let's call it “dto” (remember to resolve the package structure correctly). 
  • The Java5 Domain classes must stay on server side
  • The 'src' folder of your GWT application should then look like this:
    • src/com/testing/client/
    • src/com/testing/server/ (containing Java5 domain classes)
    • src/com/testing/dto/
    • src/com/testing/public/com.urgwt.gwt.xml
  • Your DTO classes must have the same name than the relevant Domain class (possibly with a suffix) and they must extend the net.sf.hibernate4gwt.pojo.java14.LazyPojo class :

package com.testing.dto;

// DTO class for the User domain class

public class UserDTO extends LazyPojo


  • They must also be "GWT 1.4 compatible" : no annotations, no generics and collections must be tagged with the gwt.typeArgs doclet.
4. HibernateBeanManager initialisation
  • The HibernateBeanManager instance needs the Hibernate SessionFactory to work properly.
  • You must also define a class mapper class (making the conversion between Domain and DTO class names _ useful for bean cloning), with Domain and DTO root package
Without Spring

HibernateBeanManager.getInstance().setSessionFactory(sessionFactory);
// or HibernateBeanManager.getInstance().setEntityManagerFactory(emf);

DirectoryCloneMapper cloneMapper = new DirectoryClassMapper();
cloneMapper.setRootDomainPackage(
"com.testing.server.domain");
cloneMapper.setRootClonePackage(
"com.testing.dto");
cloneMapper.setCloneSuffix(
"DTO");

HibernateBeanManager.getInstance().setClassMapper(cloneMapper);


Spring applicationContext.xml

<bean id="pojoStore"
    class="net.sf.hibernate4gwt.core.store.stateless.StatelessPojoStore" />

<bean id="classMapper"
      class="net.sf.hibernate4gwt.core.beanlib.java5.DirectoryClassMapper">
    <property name="rootDomainPackage" value="com.testing.server.domain" />
    <property name="rootClonePackage" value="com.testing.dto" />
    <property name="cloneSuffix" value="DTO" />
</bean>

<bean id="hibernateBeanManager"
      class="net.sf.hibernate4gwt.core.HibernateBeanManager">
    <property name="pojoStore" ref="pojoStore" />
    <property name="sessionFactory" ref="sessionFactory" />
    <property name="classMapper" ref="classMapper"/>
</bean>


Using Spring IoC, the HibernateBeanManager class must then be injected in every HibernateRemoteService class, for example as following :

public UserRemoteImpl()
{
   setBeanManager((HibernateBeanManager)           ApplicationContext.getInstance().getBean("hibernateBeanManager"));
}




5. RemoteService

  • Make your RemoteService implementation inherits from the HibernateRemoteService
  • Call merge method on every incoming DTO argument and clone on any outcoming Domain instance :
public class UserRemoteImpl extends HibernateRemoteService
                            implements UserRemote

{
    ...
    public UserDTO getUser(int id)
    {
         return (UserDTO) clone(_userDAO.loadSimpleUser(id));
    }
     
    public UserDTO updateUser(UserDTO userDTO)
    {   
    //    Update the user in database
    //
         user = (User)merge(userDTO);
         _userDAO.updateUser(user);
           
    //    Send back a new clone (optimistic lock)
    //
         return (UserDTO) clone(user);
    }
}

That's all :D

More...

For a more complete example, a simple sample application of each configuration is available for download !

Copyright 2007. All Rights Reserved
 Last updated : 19 June 2007