Tuesday, February 12, 2013

A simple J2EE webapplication; Netbeans + PrimeFaces + JPA

Webapplications are not my area of expertise, but once in a while I like to try some things to see how easy it would be to get something done. This time I wanted a webinterface which would be able to get/display data from a database and put stuff back in. I wanted a solution which would be quick to develop and stable in an enterprise environment. I chose to use open source software for this. I am however impatient and want quick results.

Development environment

I was looking for a development environment which would allow me to be productive; had good support for webdevelopment (wizards for generating code for me) and Maven (for easy dependency management). I started out with Eclipse. I followed the following tutorial to get it installed in Ubuntu; https://help.ubuntu.com/community/EclipseIDE. Ubuntu comes packaged with an old Eclipse version so to get the newest version working, some things needed to be done. After that I installed the Maven support and the WebToolKit (WTK) plugin. After much fiddling with the plugins, I decided to quit trying to get this to work; I couldn't get it working quickly enough and I would probably encounter other issues along the way when continuing on this path.

My second choice for IDE was Netbeans. I installed the latest version and Maven, JSF integration, Glassfish integration worked out of the box without any work. I was pleased with Netbeans.

Front-end framework

For the front-end I decided on a JSF implementation which is seen quite often nowadays. I took a look at the following comparison of JSF frameworks; http://www.mastertheboss.com/richfaces/primefaces-vs-richfaces-vs-icefaces. Based on the comparison and my bad experiences with Richfaces and server push, I decided to go with Primefaces (try something new). It had jQuery code integrated which I also liked (see for example; http://blog.primefaces.org/?p=2226). PrimeFaces has a large set of ready to use examples; http://www.primefaces.org/showcase/ui/home.jsf. This would save me a lot of work.

Back-end integration

For the database I wanted to have as little work as possible. I don't like to program things like relations, constraints, data structures, etc in the database; I was developing a web-application and the data-model, implemented as Java entities, should be sufficient definition. I chose to use the Java Persistence API (JPA) in combination with the pre-installed/configured Derby database. For the JPA implementation I chose EclipseLink since it was selected by default. Hibernate was also one of the options. I didn't do any research on the best choice for me since I wanted to quickly start developing and this worked out of the box.

Developing

The EntityManager and EntityManagerFactory

I read several tutorials and went developing. I started out with different tutorials such as  http://hendrosteven.wordpress.com/2008/03/06/simple-jpa-application-with-netbeans/ and http://www.objectdb.com/tutorial/jpa/netbeans/web. In these tutorials I encountered two very interesting and much discussed classes; EntityManagerFactory and EntityManager. These two classes make it possible to link the data model entities to the persistent storage.

Then it became a little bit more complicated. Where should I do things like starting a transaction? What should be the scope of my transaction? How should I manage the lifecycle of the EntityManager and the EntityManagerFactory? Since the transaction code could be reused among entities I decided not to put it in my entity classes. I first created an EntityUtils class (replace entity with a specific entity name) which would do entity management such as starting transactions, deleting, creating entities, performing queries on the entities. Then I found the following list of patterns; http://blog.xebia.com/2009/07/13/jpa-implementation-patterns-wrap-up/. My EntityUtils class was actually a DAO! (Data Access Object). I refactored my EntityUtils class and created a common (using type-variables) abstract class for DAO's. I still had to manually manage the transactions though.

Then I found the following tutorial;http://netbeans.org/kb/docs/javaee/javaee-gettingstarted.html. I could generate almost all the code I had previously written. Manual transaction management was not required. The DAO was however called ServiceFacade. Most likely because of the different role the class has when comparing for example Sevlet implementations to JSF implementations. Based on this last tutorial I could finish my webapplication and had an efficient mechanism to generate most of the code required to use the database from my JSF application.

I have not provided my application as an example for download since the tutorial mentioned would provide a better description. Also I will need to expand the application a little further to convince myself I'm developing efficient reusable stable code.

Conclusion

These conclusions are based on my personal experiences.

- use Netbeans instead of Eclipse in you want more functionality (being not specific here on purpose) to work out of the box
- checkout different frameworks for your JSF implementation. PrimeFaces has a lot of easily usable online examples and jQuery integration
- start a research on the EntityManager and EntityManagerFactory. using them efficiently in a Servlet differs from an efficient implementation in a JSF page.
- there is a lot of information available. getting to the relevant parts required effort

The following document contains a lot of information on J2EE; http://docs.oracle.com/javaee/6/tutorial/doc/javaeetutorial6.pdf. To get something working quickly however http://netbeans.org/kb/docs/javaee/javaee-gettingstarted.html is recommended.

No comments:

Post a Comment