Friday, October 12, 2007

Hibernate untill your ADF-faces Spring (part II)

If you are an ADF developer with an Oracle forms background, you've probably been using ADF Business Components to implement the business and integration layer of your webapplications. So have I. Hibernate was new to me, and I will try to explain just a little bit in this post. I have some very good developers in my team that do the hard work on the hibernate part of this project. I will keep it simple and just explain some of the basics.

The persistence layer contains a set of classes and components, which handle the tasks of persisting or retrieving objects to or from the underlying database. This layer includes a domain model that includes the business entities. Hibernate is a typical example for technologies used in this layer.

There is a thing to keep in mind. You don't write sql queries. The hibernate framework will generate those for you. This is called "Hibernate Query Language" (HQL). The generated Language is based on the tye of database that you are connecting to (database independent indeed !). As an Oracle developer you really have to get used to this (Why should you.... ? Just use Oracle 11g database;-)).

Ok, but how does it work ? Down here you see the content of the project:

You can see that there are three main packages:
A Dao package for the data access,
a Model package, containing the orm and the service
and a util package containing the DAOfactory and a HibernateUtil class.

How this all works together is very well explained in this document.

I'll just explain a (very) little bit on what the involved components will do for you:
Generic data access object (DAO)
The GenericHibernateDao class contains methods that are used by every DAO,
for instance the findAll and findByCriteria methods.

Entity Dao's
The specific entity Dao's extend the GenericHibernateDao and therefor inherit the generic methods. In this case the findByName method is specific for Employee and is implemented in the EmployeeDao.

Model Class
A Model Class for Employee which tells hibernate what table this is based on and that extends the OrmEmployees class. The OrmEmployees class holds the properties under Hibernate Control for Employee such as column mappings with the getters and setters.
package com.blogspot.lucbors.core.model;
import com.blogspot.lucbors.core.model.orm.OrmEmployees;

@Table(name = "EMP")
public class Employee extends OrmEmployees
* Default Constructor.
public Employee()

package com.blogspot.lucbors.core.model.orm;

imports here
* Class holds the properties under Hibernate Control for Employee.
public abstract class OrmEmployees
@Column(name = "EMPNO", unique = true, nullable = false)
private Long id;
@Column(name = "ENAME", nullable = true)
private String firstName;
@Column(name = "JOB", nullable = false)
private String lastName;

* Default Constructor.
public OrmEmployees()
/*......getters and setters go down here */

The employeeServiceImpl is used to expose the functionallity. We mapped this in the core-empdemo.xml. This is a file that tells your application exactly where to get the service that you are using (contextConfigLocation). You should put this file on your classpath.

The hibernate configurationfile (hibernate.cfg) for indicating
the database version (duh) and the mapping of classes.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
<mapping class="com.blogspot.lucbors.core.model.Employee" />

In the file you can define the database connection:
# Hibernate Database Connection #


All this is something that you configure at the beginning of the project. Normally there will be one "hibernator" to do this configuration.

I'm curious if I ever get to write part III on "Hibernate untill your ADF-faces Spring", because as we speak, there are some issues in the project. Nothing to do with technology or architecture, it's (again) all about politics..........

Thursday, October 11, 2007

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.

<value>#{empServ}</value> <!--Inject this value from Spring-->

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.
<!-- Install the Spring JSF integration -->

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

<!-- core-empdemo.xml contains Service & Dao object
configuration as well as transaction demarcation -->

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=""
<bean id="employeeService"

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: