miércoles, 22 de junio de 2011

JSF-Spring-JPA(Hibernate) Integration


Some people asked me to translate this post, so, this is the english version of a previous post I did some time ago about JSF-Spring-Hibernate(JPA) Integration.

Some months ago, Profesor Java (a mexican Java evangelization project) invited me to give a talk about "How to integrate java frameworks: JSF-Spring-JPA(Hibernate)" and thank you so much to all of the people who attended from places like: Argentina, México, Colombia, El Salvador, Ecuador, Perú... it´s really nice to know that there are latin american people interested in generate Open Source projects with collaborative philosophy. Here we are to give us a hand ;)

Let´s beggin.

The architecture used in the example is a "Layered architecture":



As you can see, we use this architectural model because it promotes a well done Software Development, and we will obtain great benefits such as:

1) A clear separation between layers to delegate specific functions in order to avoid a highly cohesive and "spaghettized" code .

2) To Make our aplication scalable, and it will be easy to migrate to new technologies with a "lego vision" (see image) in wich we remove the piece we do not need to replace it with a new one that fits our needs; for example, in this layered architecture: in the Presentation Layer we could migrate from JSF to Flex as easy as change legos, and we don´t even need to modify the source code from the rest of the layers. Likewise, in Business Logic Layer we could to migrate from Spring to EJB or Hibernate to Toplink (in Data Access Layer). As you can see, we can make significant changes in our application without affecting the other layers and most of all the code that we already have. Of course, there will be the need of some aditional chages, but they will be insignificant in relation to change a whole aplication from scratch.

At this point we have a clear vision of the stage in wich we are going to walk, so, let´s check the example we are going to develop in this post:

Our main goal is to develope a web aplication that runs on a web server such as: Tomcat, Glassfish, Jboss... and that aplication will interact with a Datebase (in this case MySQL) performing comun actions like: insert, retrieve and display information, all of this through the following jee frameworks: JSF-Spring-Hibernate (through JPA). The User interface looks like this:


OK, Let´s start!

1) We need to create an empty Database named "frameworks". We´ll use Hibernate´s potential to create tables.
2) We create the Hibernate configuration file:



3) We need to to reference Hibernate into the Spring config file:



4) Create the "Persona" entity (using JPA annotations). Note: It´s really important to implement the Serializable Interface when you are workin with entities that will be persisted as a records of a database.



5) We need to add the mapping to the entities into the Hibernate config file and let´s create automaticly the tables of "frameworks" database.

6) Crete the PersonaDAO Interface in wich we mention the functional features of insertion and retrieve .



7) Let´s implement Interface PersonaDAO, here we develope the bussines logic of our aplication, you can see how Spring help us on this.



8) In Spring config file we need to reference the bean than implemnts PersonaDAO. Note: Spring is a bean-factory.



9) Create Backing Beans (to JSF support). In this context you must understand an important Spring concept here: Dependency Injection (DI).



10) Create a JSF config file. Note: You could use annotations instead of using the JSF config file. EnlaceNote: you can see another post here about JSF-Hibernate integration in wich I used JSF 2 with anotations.



11) Create a JSF page.



12) The final step: Testing the aplication on a Web server (in my case Tomcat).

Source Code (Eclipse project): here Let me know if you have any problem ;)
Libraries here
Power Point slides here

CONCLUSION.

Lightweight frameworks like Hibernate or Spring has been amply used because they appeared as a serious solution for JEE´s problems some year ago. Hibernate is a robust solution has served as the basis for the creation of the persistence standar: jpa. Spring is a framework that grows exponentially every day because and apart from the characteristics that made him famous: Dependency Injection and Inversion of Control (being a Bean Factory), now provides a wide variety of functionality into modules such as Spring Security, Spring AOP, Spring MVC, etc.. so it has become serious competition to the EJB, for tath reasons, both of them, Spring and Hibernate are two very reliable options that should be taken into account by the architects when implementing Java web solutions.

Note: hi5, the social networks uses Spring in its architecture.

4 comentarios:

Anónimo dijo...

Hola, enhorabuena por el post. Me ha servido de mucha ayuda. ¿ podrías colgar las librerías en otra web?... es que como megaupload la han cerrado...
gracias

Luis

Santiago Tapia dijo...

Saludos, el link de las librerías está actualizado ya. Gracias por leer el blog.

Mohammed Amine H. dijo...

please sir what are the benefits of using jsf with spring knowing that spring has module who does the same work in here (spring mvc) ? and is this architecture modern ? thanks

Santiago Tapia dijo...

Hi Mohammed. Thanks for reading my blog.
Yeap, you´re absolutely right, using SpringMVC would be a smart decision in real scenarios but when I wrote the post (March/2011) my main goal in the moment to Integrate this three frameworks was to take advantage of that great Spring´s bennefit: Dependency Injection and this post was made because of a request my readers asked me ;)
Yes, Spring is great but if you let me, I recomend you to start using the new features of the JEE6 specification instead of Spring, because the first one is a standar used all over the world,
in fact, at this moment I see lots of bennefits of JEE6 over Spring.
At this moment, I´m dedicating my time to JEE6 and the next posts in my blog will be dedicated to this amazing technology.
Greetings from South America ;)