Skip to main content

Hibernate untill your ADF-faces Spring (part I)

It's october and summer ended a couple of weeks ago. A lot of rodents and bears prepare for winter and hibernation. So will I! I got involved in a project in which creativity was pushed to the limit in a very early stage.

We wanted to do a project with ADF (full framework) for one of our customers. This approach wasn't allowed. I've been mailing and talking about this case with some people at oracle (Steve Muench a.o.) but it is at it is...Either you like it or you don't. There are customers who, for whatever good reason, don't want to use ADF. John Stegeman wrote a nice entry on his blog about the pro's and cons, and about 'pride and prejudice' concerning JDeveloper and ADF.

In this case however, instead of forbidding the use of any part of the ADF, my customer decided to allow a Poc (Proof of Concept) in which I (and my co-workers) were allowed to use the ADF-faces part of the framework.

The consequence was that we had to think of an alternative application architecture.
Because of customer standards we finally decided to develop a system that in the end will cover two layers: a GUI made up of ADF-faces and a backend made up with spring hibernate talking to an Oracle database. This is not something very special. In the end it is just java talking to java, and this should not be a problem, but we wanted to make sure that this approach worked, and we never tried before. In the company I currently work for, the Oracle Unit does JDeveloper-ADF projects and the Java Unit does everything except Oracle........I'm trying to bring those two together.

We did an internal pilot on the EMP table in the HR schema in the Oracle database.

In the next couple of posts on this blog I'll try to give you a step by step guide of this approach. You can see the draft architecture in the picture below, in which I also show you which post will cover which part of the solution.



Because this blog was created to talk mainly about ADF, I will tell you the most important part (ADF-faces) in this part of this series. So that if you don't want to know how hibernate works, you still know how to use it in your ADF-faces front end. Here is what you need: Have your J2EE developers create a jar-file with all the logic concerning the Business and Integration layer. (I talk about how you could make this layer in Part II.). For now lets asume it's all created for you.

Now, the first thin you have to do is to include this jar in your ADF-faces project. Next you create a backingbean that uses the EmployeeService to find all employees, create a Page with an ADF-table, bind this table to the backingbean, run the page, and it should work.

The backingbean
package com.blogspot.lucbors.view.backingbeans;

import java.util.List;
import javax.faces.context.FacesContext;
import com.blogspot.lucbors.core.model.service.EmployeeService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

public final class FacingHibernateBackingBean {

private EmployeeService empServ;
private ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());

public FacingHibernateBackingBean() {
}

public EmployeeService getempServ() {
return empServ;
}

public void setempServ(EmployeeService empServ) {
this.empServ = empServ;
}

public List getAllEmployees() {
empServ = (EmployeeService)ctx.getBean("employeeService");
return this.empServ.getAllEmployees();
}
}



Getters and setters shouldn't be new to you, so I'll just talk about the most important part of this bean: public List getAllEmployees(). This method is an excellent example of how you can build your apps without knowing how the logic in the business layer is implemented. The only thing you have to know is that there is a service with a method getAllEmployees, and that this method returns you a list with all employees. You don't even need to know how hibernate works because you don't care as long as the service you call returns the list of Employees to you.

Now you have to set the properties in the faces-config, so that your bean knows where to get the Service.

<managed-bean>
<managed-bean-name>FacingHibernate</managed-bean-name>
<managed-bean-class>
com.blogspot.lucbors.view.backingbeans.FacingHibernateBackingBean
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>empServ</property-name>
<value>#{empServ}</value> <!--Inject this value from Spring-->
</managed-property>
</managed-bean>


Configuring your application to use Spring and Hibernate
Make sure that your application knows that you are using Spring for integration. So in the faces-config you should declare an application.
<application>
<!-- Install the Spring JSF integration -->
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>

In you deployment descriptor (web.xml) you have to create an extra context-parameter, an extra filter and an extra listener.

<context-param>
<param-name>contextConfigLocation</param-name>
<!-- core-empdemo.xml contains Service & Dao object
configuration as well as transaction demarcation -->
<param-value>
classpath:core-empdemo.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>HibernateFilter</filter-name>
<filter-class>
com.blogspot.lucbors.view.utils.HibernateSessionRequestFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>HibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


The core-empdemo.xml is a file that tells your application exactly where to get the service that you are using. You should get this file from your hibernate developer and place it on your classpath.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="employeeService"
class="com.blogspot.lucbors.core.model.service.impl.EmployeeServiceImpl"/>
</beans>


So now you are all set. It looks as if it is kind of compicated, but remember, it's configuration, and you only have to do that when you start a project ! Once you have it all up and running, just leave it alone.

The next thing you do is create a page with an ADF-table on it. You then bind that table to the backingbean.



Run the page, and it's working (if you included the Spring and hibernate libraries in your project)!!

Next time I'll write about the business and integration layer a little bit.

usefull resources:
http://wiki.apache.org/myfaces/Hibernate_And_MyFaces

Comments

Anonymous said…
Where I work, we were using this stack in JDeveloper, but have since moved to JBoss Developer Studio.

Have you tried using Spring Managed backing beans instead of configuring your beans in faces-config.xml. To do this, use

org.springframework.web.jsf.DelegatingVariableResolver

as a variableResolver in your faces-config.xml and you should be good to go.
Neuquino said…
what aabout HibernateSessionRequestFilter class? How do you implement this class?
Ravi said…
If i had to use my Custom library (mySpring-Hibernate.jar) in ADF application do i need to include the weblogic.xml file as I do have in my other web applications which are refering to (mySpring-Hibernate.jar) configured as libraries in my weblogic server
Ravi said…
If i had to use my Custom library (mySpring-Hibernate.jar) in ADF application do i need to include the weblogic.xml file as I do have in my other web applications which are refering to (mySpring-Hibernate.jar) configured as libraries in my weblogic server

Popular posts from this blog

ADF 12.1.3 : Implementing Default Table Filter Values

In one of my projects I ran into a requirement where the end user needs to be presented with default values in the table filters. This sounds like it is a common requirement, which is easy to implement. However it proved to be not so common, as it is not in the documentation nor are there any Blogpost to be found that talk about this feature. In this blogpost I describe how to implement this.

The Use Case Explained
Users of the application would typically enter today's date in a table filter in order to get all data that is valid for today. They do this each and every time. In order to facilitate them I want to have the table filter pre-filled with today's date (at the moment of writing July 31st 2015).


So whenever the page is displayed, it should display 'today' in the table filter and execute the query accordingly. The problem is to get the value in the filter without the user typing it. Lets first take a look at how the ADF Search and Filters are implemented by the f…

ADF 12.2.x : Conditional Showing Message Instead of List of Values Popup

Today I had to fix an issue in one of our ADF applications. For those of you working with ADF (as I do too occasionally) this might be valuable, so I decided to share this. The application is based on an old Oracle Forms applications and I had to implement the following functionality:

FRM-41830 : List of Values contains no entries.

In other words (more ADF like) : If you are about to render a List of Values and that List of Values contains no rows, just show a message instead of showing the List of Values.

In this post I will describe how I was able to implement this although it turned out to be a challenge.
Below are the steps that I took. Note that the Application can be downloaded from GIT.

1) I use a UI pattern where we use readonly table and do edits in a popup. That means that any List of Values will add an additional popup to the UI.



2) The List of Values that I use in this sample only shows entries where the minimum salary is less then the employees current salary. Not that …

ADF 11g Quicky 3 : Adding Error, Info and Warning messages

How can we add a message programatically ? Last week I got this question for the second time in a months time. I decided to write a short blogpost on how this works.

Adding messages is very easy, you just need to know how it works.
You can add a message to your faces context by creating a new FacesMessage. Set the severity (ERROR, WARNING, INFO or FATAL ), set the message text, and if nessecary a message detail. The fragment below shows the code for an ERROR message.

1: public void setMessagesErr(ActionEvent actionEvent) {
2: String msg = "This is a message";
3: AdfFacesContext adfFacesContext = null;
4: adfFacesContext = AdfFacesContext.getCurrentInstance();
5: FacesContext ctx = FacesContext.getCurrentInstance();
6: FacesMessage fm =
7: new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, "");
8: ctx.addMessage(null, fm);
9: }


I created a simple page with a couple of buttons to show the result of setting the message. When the butto…