UPDATE: 05.08.2013
If you came to this blog in order to learn how to manage Data transfer from Server layer to the GWT layer, please note that since 2010 GWT team has introduced new and comfortable way to do that. The mechanism is called Request Factory and you can read more about it here: http://www.gwtproject.org/doc/latest/DevGuideRequestFactory.html.
If you run to any problems you can check my other blog post where I explain how the most common update/save workflow works in Request Factory: exciting life of entity proxies.
UPDATE: 14.10.2010 – official GWT 2.1 .RC release with Request Factory
GWT Gilead tutorial – what for?
I) I assume you already know :
- GWT basics
- how to create widgets on client side,
- how to make Remote Procedure Calls (via GWT RPC),
- usage of GEP plugin to configure and manage your project in Eclipse
- Hibernate, mapping entities with Annotations
- HSQL database
II) Technologies used:
- GWT 2.0.4
- Gilead 1.3.2 download from : http://sourceforge.net/projects/gilead/files/
- Hibernate 3.x
- gwt-log 3.0.3 ( but is not required – used just for easy way of debug) download from : http://code.google.com/p/gwt-log/downloads/detail?name=gwt-log-3.0.3.jar&can=2&q=
III) Tools used:
- Eclipse Galileo
- Google Eclipse Plugin (GEP)
- HSQL database
IV) Tutorial
A) getting simple GWT project
- Download this project : http://dl.dropbox.com/u/11217505/StudentManagerWithoutGilead.zip . Unpack it and import to Eclipse. This project goal is to manage professors with their list of students. Project consists of elements:
- Domain classes: Professor and Student. Notice that Professor has a List of students which is annotated as OneToMany with fetch=FetchType.LAZY.
- Services: StudentManagerService, StudentManagerServiceAsync, StudentManagerServiceImpl. Created with Gwt-rpc mechanism. They are using DAO classes for saving and retrieving data about professors and students from the database.
- DAOs – this is the direct layer which handles the database.
- Client code is written in one class StudentManager. User see three panels where he can add professor, student and connect student with a professor.
- StudentManager.gwt.xml is configuring gwt compiler.
- configuration files :
-
hibernate.cfg.xml is configuring connection to database and hibernate behaviour
-
log4j.properties is configuring logging appenders.
-
- start the HSQL database
- Run project in debug mode
- Open application in the browser ( using the link given by debugger console)
- Try to add student, then to add professor. The third panel should contain 2 combo box lists with students and professors you have added. Choose professor and a student from lists and then click “Add connection” button. This operation should fail on the server. After trying to add this connection I have got exception:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
- Now we will add Gilead framework to this simple GWT project in order to have possibility to serialize our entities without problems.
B) Adding Gilead support.
- download Gilead.
- Unpack the downloaded archive.
- In the archive you will find set of libraries. Copy specified jars into your war/WEB-INF/lib folder :
- Edit “StudentManager.gwt.xml” file adding line:
<inherits name='net.sf.gilead.Gilead4Gwt' />
- Make entity classes Professor and Student extend the net.sf.gilead.pojo.gwt.LightEntity.
import net.sf.gilead.pojo.gwt.LightEntity; @Entity public class Professor extends LightEntity implements Serializable{
- Now you have to change Service implementation class: StudentManagerServiceImpl
- Make this class extend the net.sf.gilead.gwt.PersistentRemoteService
- You also need to initialize so-called BeanManager. This is class which is helping with serialization of your objects.
public class StudentManagerServiceImpl extends PersistentRemoteService implements StudentManagerService { StudentDao studentDao = new StudentDao(); ProfessorDao professorDao = new ProfessorDao(); private Log log = LogFactory.getLog(StudentManagerServiceImpl.class); private static HibernateUtil gileadHibernateUtil = new HibernateUtil(); public StudentManagerServiceImpl() { initHibernateUtil(); } private void initHibernateUtil() { log.debug("initializing hibernate persistance for gilead"); gileadHibernateUtil.setSessionFactory(AbstractDao.getSessionFactory()); PersistentBeanManager persistentBeanManager = GwtConfigurationHelper .initGwtStatelessBeanManager(gileadHibernateUtil); setBeanManager(persistentBeanManager); log.debug("persistentBeanManager initialized"); } ... }
- Run your project one more time. Go to the browser and try to add professor – student connection. If you have made all the steps correctly you shouldn’t see any LazyInitializationException and have no more problems with serialization:)
You can download already configured last version of project example from here:
http://dl.dropbox.com/u/11217505/StudentManager.zip
V) Links
- Gilead home page : http://noon.gilead.free.fr/gilead/index.php?page=gwt
- GWT home page: http://code.google.com/webtoolkit/
- http://code.google.com/p/gwt-log/
- http://code.google.com/webtoolkit/articles/using_gwt_with_hibernate.html information from google about different ways to integrate hibernate with GWT. They are also posting example gilead project, which I used as a source of examples. Unfortunately this project is with old GWT version.
Leave a reply to gbougeard Cancel reply