Home Struts Hibernate-spring Understanding Spring Struts Hibernate DAO Layer
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Understanding Spring Struts Hibernate DAO Layer

Advertisement
In the example you'll discuss about 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.

 

Login.java

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;
	}

}

Login.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<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" />
</id>

<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" />


</class>
</hibernate-mapping>


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-

 

applicationContext-hibernate.xml

<?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 -->
   <beans>
	<bean id="propertyConfigurer"
	class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="location">
	<value>/WEB-INF/jdbc.properties</value>
	</property>
   </bean>	

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

    </bean>

</beans>

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

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource">
		<ref local="dataSource" />
   </property>
   <property name="mappingResources">
	<list>
		<value>roseindia/dao/hibernate/Login.hbm.xml</value>
	</list>
   </property>
 <property name="hibernateProperties">
  <props>
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    <prop key="hibernate.show_sql">true</prop>
  </props>
 </property>
</bean>


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

ApplicationDao.java

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;
		}

		smt.close();
		rs.close();
		conn.close();

		return valid;

	}
}

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 17, 2005

Related Tutorials

Ask Questions?    Discuss: Understanding Spring Struts Hibernate DAO Layer   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Vijay Nataraj
October 20, 2011
Integration with GWT

Hi,i would like to do the web application project with below mentioned combination. So plz send me the tutorial which will clearly explain GWT-Spring/ Hibernate-Mysql.
Martin
April 30, 2012
not very good code

As there is no exception handling and try catch finally block to always close connection.
DMCA.com