Skip to main content

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;

@Entity
@Table(name = "EMP")
public class Employee extends OrmEmployees
{
/**
* Default Constructor.
*/
public Employee()
{
super();
}
...................

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

imports here
......................
/**
* Class holds the properties under Hibernate Control for Employee.
*/
@MappedSuperclass
public abstract class OrmEmployees
{
@Id
@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()
{
super();
}
/*......getters and setters go down here */
}

core-<app>.xml
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"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
...................
<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" />
</session-factory>
</hibernate-configuration>

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

hibernate.connection.url=jdbc:oracle:thin:@localhost:1521:xe
hibernate.connection.username=hr
hibernate.connection.password=hr
hibernate.default_schema=hr

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..........

Comments

Anonymous said…
Luc,

Is it possible for you to publish the source code for this POC? We have a similar requirement at our company (primarily an open source initiative) and could benefit from your work.

If can provide me your email, I can send you a detailed email on why we need this capability for our open source initiative.

Thank you.

John Skillings
jskillings07 at gmail dot com
Arnald said…
Luc,

Thats a neat consolidation...

Arnald

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…