Understanding Spring Struts Hibernate DAO Layer

In the example you'll discuss about Spring Struts Hibernate DAO Layer

Understanding Spring Struts Hibernate DAO Layer

Understanding Spring Struts Hibernate DAO Layer


In this section we will understand how Spring Hibernate and Struts will work together to provide best solution for any web based application.


Understanding Spring Struts Hibernate DAO Layer


The Data Access Object for this application is written using hibernate framework. As you know hibernate is a collection of libraries and xml file used for mapping object oriented domain model to a relational database. It is a Open Source available for object relation database mapping written in java. It offers facilities such as connection pooling, transaction management, updating, insertion and deletion of data very easily.


In hibernate application separate class is used to represent table in the database. Each table have their corresponding class hibernate and mapping are done with the help of .hbm.xml file. Each field of the table is mapped with .hbm.xml file. An example of Login.hbm.xml is given below with their class.



package roseindia.dao;

import java.io.Serializable;

public class Login implements Serializable {

	/** identifier field */
	private Integer id;

	/** persistent field */
	private String loginid;

	/** persistent field */
	private String password;

	public Login() {

	/** full constructor */
	public Login(Integer id, String loginid, String password) {
		this.id = id;
		this.loginid = loginid;
		this.password = password;

	public Integer getId() {
		return id;

	public void setId(Integer id) {
		this.id = id;

	public String getLoginid() {
		return loginid;

	public void setLoginid(String loginid) {
		this.loginid = loginid;

	public String getPassword() {
		return password;

	public void setPassword(String password) {
		this.password = password;



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"

<hibernate-mapping auto-import="true" default-lazy="false">

<class name="roseindia.dao.hibernate.Login" table="login">

<id name="id" type="java.lang.Integer" column="id">
<generator class="increment" />

<property name="loginid" type="java.lang.String" column="loginid"
not-null="true" unique="true" length="20" />
<property name="password" type="java.lang.String" column="password"
not-null="true" length="20" />

<property name="email" type="java.lang.String" column="email"
not-null="false" length="30" />
<property name="address" type="java.lang.String" column="address"
not-null="false" length="60" />

<property name="phno" type="int" column="phno" not-null="false"
length="11" />


Let us consider the developments of hibernate data access object. The Hibernate DAO consists applicationContext-hibernate.xml file, which holds the all the information related to database connection, such as connection URL, database driver, user name, password, size of connection pool, location of .hbm file etc. Please consider the simple applicationContext-hibernate.xml file is given below-



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
	<!--locating  JDBC properties file. This file contains JDBC driver, connection URL, user name and password -->
	<bean id="propertyConfigurer"
	<property name="location">

   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName">
	<property name="url">
	<property name="username">
	<property name="password">



<!--Mapping to .hbm file-->

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource">
		<ref local="dataSource" />
   <property name="mappingResources">
 <property name="hibernateProperties">
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    <prop key="hibernate.show_sql">true</prop>

After this write a DAO class for, which performs operation the database. Please consider a simple DAO class given below


public class SpringHibernateDAOImpl extends HibernateDaoSupport {

	public boolean checkUserLogin(String strUserName, String strPassword)
			throws DataAccessException, java.sql.SQLException {
		boolean valid = false;
		Connection conn = this.getSession().connection();
		// Write jdbc code to validate the user against database
		Statement smt = conn.createStatement();
		ResultSet rs;
		// write select query for checking password

		// Query String 
		String query = "select id from login where loginid='" + strUserName
				+ "' and password='" + strPassword + "'";
		rs = smt.executeQuery(query);

		if (rs.next() == true) {
			valid = true;

		} else {
			valid = false;


		return valid;