Getting
started (stateless mode) |
This
sections describes the simplest way to enable hibernate4gwt for a
stateless application.
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 (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'/>
<!-- 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'/>
<!-- Servlet path
-->
...
</module>
|
3. Domain classes
- Put 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
- Make all
your
domain classes inherit
from “net.sf.hibernate4gwt.pojo.java14.LazyPojo”
(for
GWT 1.4) or
“net.sf.hibernate4gwt.pojo.java5.LazyPojo” (for
GWT 1.5).
public class User extends LazyPojo
|
4.
HibernateBeanManager initialisation
- The
HibernateBeanManager instance needs the Hibernate
SessionFactory (or its JPA EntityManagerFactory counterpart) to work
properly. You have to set it in your server
initialisation code :
Without
Spring
HibernateBeanManager.getInstance().setSessionFactory(sessionFactory);
// or
HibernateBeanManager.getInstance().setEntityManagerFactory(emf);
|
Spring
applicationContext.xml
<bean id="hibernateBeanManager"
class="net.sf.hibernate4gwt.core.HibernateBeanManager">
<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.
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 !
|