Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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.

 

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

                          

» View all related tutorials
Related Tags: c hibernate web spring application io this app ie add nat to bi e il it section li in ca

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.