Implementing Data Access Layer with Hibernate

In this application we are using Hibernate to implement data access layer.
Hibernate is an open source O/R mapping framework that handles all the
persistence logic.
Hibernate supports all the major database available in the market. The
Hibernate Query Language is an object-oriented extension to SQL, which can be
extensively used to save and retrieve the data in the form of Java objects from
database. Hibernate also supports association, inheritance, polymorphism,
composition and also the Java Collection framework.
Data Access Object (DAO)
In this application we have used the DAO pattern. The DAO pattern abstracts and encapsulates all access to the data source.
Our application has one DAO interface: HibernateSpringDAO. The implementation
classes of it is HibernateSpringDAOImpl that contains Hibernate-specific logic to manage and persist data.
Here is the code of HibernateSpringDAO.java file:
/*
* Created on 02-July-2007
*
*/
package net.roseindia.dao;
import java.util.*;
import org.springframework.dao.DataAccessException;
import net.roseindia.dao.hibernate.*;
/**
* @author Deepak Kumar
*
*/
public interface HibernateSpringDAO {
/**
* Retrieve all <code>true</code>/<code>false</code> from the datastore.
* @return a <code>true</code> or <code>fasel</code>.
*/
public User checkUser(String strUserName) throws DataAccessException,java.sql.SQLException;
/**
* Retrieve all <code>true</code>/<code>false</code> from the datastore.
* @return a <code>true</code> or <code>fasel</code>.
*/
public User validateUser(String strUserName,String password) throws
DataAccessException,java.sql.SQLException;
/**
* Saves User object to the datastore.
*
*/
public void addUser(net.roseindia.dao.hibernate.User obj) throws DataAccessException;
}
|
Here is the code of HibernateSpringDAOImpl.java file
that actually implements the logic:
package net.roseindia.dao;
import java.util.*;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.hibernate.criterion.*;
//Java Imports
import java.sql.*;
import javax.servlet.http.HttpSession;
//Project Imports
import net.roseindia.dao.hibernate.*;
/**
* @author Deepak Kumar
* Created on 02-July-2007
*/
public class HibernateSpringDAOImpl extends HibernateDaoSupport
implements HibernateSpringDAO {
public User checkUser(String strUserName)
throws DataAccessException, java.sql.SQLException {
User obj = null;
DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
criteria.add(Expression.eq("userName", strUserName));
List objs = getHibernateTemplate().findByCriteria(criteria);
if ((objs != null) && (objs.size() > 0)) {
obj = (User) objs.get(0);
}
return obj;
}
public User validateUser(String strUserName,String password)
throws DataAccessException, java.sql.SQLException {
User obj = null;
DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
criteria.add(Expression.eq("userName", strUserName));
criteria.add(Expression.eq("userPassword", password));
List objs = getHibernateTemplate().findByCriteria(criteria);
if ((objs != null) && (objs.size() > 0)) {
obj = (User) objs.get(0);
}
return obj;
}
public void addUser(net.roseindia.dao.hibernate.User obj)
throws DataAccessException {
getHibernateTemplate().save(obj);
}
}
; |
Database Design
Our application contains only one table whose structure are as follows:

In the next section we will integrate all the
components.

|