Integrating JSF and Hibernate

As you probably know, Hibernate is one of the most powerful Java-based ORMs in the market. Since JSF doesn't provide support for manipulating databases (it acts in the presentation tier), it is normal to think of a solution to accomplish this job in a JSF web application (this is the Integration tier)—and this solution may be Hibernate. In this recipe we will discuss this approach.

Getting ready

The latest Hibernate distribution is available at https://www.hibernate.org/. If you are using NetBeans, then a Hibernate library is available in the default libraries.

How to do it...

Normally, JSF can call Hibernate classes (objects mapped to tables) from managed beans exactly like any other classes. In other words JSF will not be aware that Hibernate is behind the scene, since no configuration is required, no injection or annotations, just the right imports. We can instantiate a Hibernate class (usually a fooHome class) and we can call its methods (usually, methods like persist, delete, merge, findByExample, and so on) just as we call a non-Hibernate Java class. So, as you can see, there is no magic, no hidden tips, just pure and simple programming. But, is this a good programming technique?

Since we want to integrate Hibernate classes right in the managed beans, it means that a part of our business logic will reside in managed beans, which is not a good technique, since managed beans are dedicated to managing the UI components of JSF pages. A more realistic solution would be to provide a business logic tier, as EJB for example, between Presentation tier and Integration tier. In this approach, JSF managed beans will inject EJB, which will make use of Hibernate's power.

How it works...

Well, as we said it works, it but is not recommended! Therefore, a direct integration of JSF and Hibernate is not recommended since it is a bad programming technique which is compresses the Business logic tier with the presentation tier.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset