Ask Questions?

View Latest Questions


Data Access Object
Posted on: March 9, 2011 at 12:00 AM
Data Access object design for the shopping cart application

Creating Data Access Object (DAO) Design Pattern

The Data Access Object is a design pattern, which consists java classes and methods to access the data. It provides a technique to separate the object persistence and data access logic. All the data access codes are written inside a dao class and it provides interface to access data.

The DAO given with application, uses hibernate to access data from database. Hibernate contains java some classes and .xml files. To make a Hibernate Dao at first you need to make hibernate.cfg.xml. This file contain all the information required to connect to a database.

Following are the properties of this file.

     <property name="hibernate.connection.driver_class">Database Driver Name</property>
     <property name="hibernate.connection.url">Connection URL of the database</property>
     <property name="hibernate.connection.username">User Name</property>
     <property name="connection.password">Password</property>
     <property name="connection.pool_size">Size of the connection pool</property>
     <property name="hibernate.dialect">Dialect of the using database</property>
     <property name="show_sql">show/hide SQL query</property>
     <property name="">used to map with the contact table</property>
     <mapping class="path of the database table" />
hibernate.connection.driver_class- Whenever you wish to connect to the database, you fist need to load the driver of that database. Every database have their own drivers. This property  used to load the driver of the database.
hibernate.connection.url - This property takes the connection URL of the database. like jdbc:/mysql:// If you database is on local host then you can write jdbc:/mysql://localhost/databaseName.
hibernate.connection.username - This attribute takes user name of the database.
connection.password- This attribute takes the password of the user name
connection.pool_size- This attribute takes size of connection pool in number.
hibernate.dialect - This attribute takes the dialect of the database. Hibernate supports many database. Every database have their own dialect name. Example for mysql we write org.hibernate.dialect.MySQL5Dialect. This actually tells the hibernate that we are using this database.
show_sql- This asks that whether to print the query generated by the hibernate, It takes the boolean value either true or false. This property takes the  create, create-drop, update, validate, none. When we pass the create the it create the table. When we pass none it does nothing.
After that we map the table class this the hibernate using <mapping class="class name with full path">
In hibernate you need to write a class corresponding to every table. You can map with that table by using xml file or by annotation. We have used annotation for mapping class to hibernate as.
package dao.tables;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "adminuser")
public class Adminuser implements Serializable {
	/** identifier field */
	private Integer id;
	/** persistent field */
	private String userid;
	/** persistent field */
	private String password;

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

	/** default constructor */
	public Adminuser() {

	@Column(name = "id")
	public Integer getId() {

	public void setId(Integer id) { = id;

	@Column(name = "userid")
	public String getUserid() {
		return this.userid;

	public void setUserid(String userid) {
		this.userid = userid;

	@Column(name = "password")
	public String getPassword() {
		return this.password;

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

The @Entity, @Table(name = "adminuser") maps the class with the database table. The @Column(name = "userid") maps with column.

Now after this make a connection factory/ or hibernate util class to which only contain the method for getting connection. To get a connection with the database hibernate uses ConnectionFactory class. Following is a code to get a connection with the database using annotation.
private static SessionFactory sessionFactory;

	static {
		try {
			sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

		} catch (Exception e) {

	public static synchronized SessionFactory getSessionfactory() {
		return sessionFactory;

After getting the connection you need to write class which contains methods on the database. An example of the simple method is given below
HibernateUtil util = new HibernateUtil();

	public List showAll() {

		Session session = util.getSessionfactory().openSession();
		Transaction transaction = null;
		List users = new ArrayList<User>();
		try {
			transaction = session.beginTransaction();
			users = session.createQuery("from User").list();
		} catch (Exception e) {
		} finally {
		return users;

The Complete code of the application is given in the end of this tutorial.

Related Tags for Data Access Object :