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

Getting started (dynamic proxy)

This sections describes the simplest way to enable hibernate4gwt for a stateless application, based on dynamic proxy generation, and so remove the LazyPojo technical inheritance.

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.
  • Edit your gwt xml file to inherit from hibernate4gwt, declare the domain package and GWT Proxy Generator (see below)
GWT 1.4
<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='domain'/>
     <source path='client'/>

     <!-- Proxy generator -->
     <generate-with class="net.sf.hibernate4gwt.rebind.Gwt14ProxyGenerator">
         <when-type-assignable class="java.io.Serializable" />
     </generate-with>

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

GWT 1.5
<module>
     <!-- Inherit the core Web Toolkit stuff.          -->
     <inherits name='com.google.gwt.user.User'/>
     <inherits name='net.sf.hibernate4gwt.Hibernate4Gwt15'/>
     <!-- Uncomment if your Domain class use JPA mapping annotations
     <inherits name='net.sf.hibernate4gwt.emul.java5.ejb3.Ejb3'/> -->

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

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

     <!-- Proxy generator -->
     <generate-with class="net.sf.hibernate4gwt.rebind.Gwt15ProxyGenerator">
         <when-type-assignable class="java.io.Serializable" />
     </generate-with>


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

3. Domain classes
  • IPut all your domain class in a shared folder between client and server : let's call it “domain” (remember to resolve the package structure correctly). The 'src' folder of your GWT application should then look like this:
    • src/com/testing/client/
    • src/com/testing/server/
    • src/com/testing/domain/
    • src/com/testing/public/com.urgwt.gwt.xml
  • Your domain classes must implements the Java “Serializable” interface : it is used as a marker for Java serialization and is required both for GWT and Hibernate beans.

public class User implements Serializable


4. HibernateBeanManager initialisation
  • The HibernateBeanManager instance needs the Hibernate SessionFactory (or its JPA EntityManagerFactory counterpart) to work properly. 
  • It also needs a ProxyClassMapper to properly use proxy in clone and merge operations
  • You have to set it in your server initialisation code :
Without Spring

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

HibernateBeanManager.getInstance().setClassMapper(new ProxyClassMapper());


Spring applicationContext.xml


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

<bean id="classMapper"
        class="net.sf.hibernate4gwt.core.beanlib.mapper.ProxyClassMapper" />
   
<bean id="hibernateBeanManager"
      class="net.sf.hibernate4gwt.core.HibernateBeanManager">
    <property name="pojoStore" ref="pojoStore" />
    <property name="classMapper" ref="classMapper" />
    <property name="sessionFactory" ref="sessionFactory" />
</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. Client side proxy initialisation

Because hibernate4gwt proxy generation is based on GWT generators, your code must force proxy creation by explicitly calling GWT.create method for each domain class. The best place to do this is probably on your application initialization code :
 

GWT.create(User.class);


6. RemoteService


  • Just make your RemoteService implementation inherits from the HibernateRemoteService instead
public class UserRemoteImpl extends HibernateRemoteService
                            implements UserRemote

{
    ...
    public User getUser(int id)
    {
         return _userDAO.loadSimpleUser(id);
    }
     
    public User updateUser(User user)
    {
          
    //    Update the user in database
    //
         _userDAO.updateUser(user);
           
    //    Send back a new clone (optimistic lock)
    //
         return 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 : 22 September, 2008