Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data access object (DAO) in Java

Tags:

java

dao

I was going through a document and I came across a term called DAO. I found out that it is a Data Access Object. Can someone please explain me what this actually is?

I know that it is some kind of an interface for accessing data from different types of sources, in the middle of this little research of mine I bumped into a concept called data source or data source object, and things got messed up in my mind.

I really want to know what a DAO is programmatically in terms of where it is used. How it is used? Any links to pages that explain this concept from the very basic stuff is also appreciated.

like image 306
Vasanth Nag K V Avatar asked Oct 03 '13 08:10

Vasanth Nag K V


People also ask

What is a DAO object in Java?

What is DATA ACCESS OBJECT (DAO) - It is a object/interface, which is used to access data from database of data storage. WHY WE USE DAO: To abstract the retrieval of data from a data resource such as a database. The concept is to "separate a data resource's client interface from its data access mechanism."

What is DAO and DTO in Java?

DAO is a class that usually has the CRUD operations like save, update, delete. DTO is just an object that holds data. It is JavaBean with instance variables and setter and getters. The DTO is used to expose several values in a bean like fashion.

What does a data access object do?

In software, a data access object (DAO) is a pattern that provides an abstract interface to some type of database or other persistence mechanism. By mapping application calls to the persistence layer, the DAO provides some specific data operations without exposing details of the database.

What benefits does data access object DAO pattern provide?

DAO design pattern allows the JUnit test to run faster as it allows to create Mock and avoid connecting to the database to run tests. It improves testing because it's easy to write a test with Mock objects, rather than an Integration test with the database.


2 Answers

The Data Access Object is basically an object or an interface that provides access to an underlying database or any other persistence storage.

That definition from: http://en.wikipedia.org/wiki/Data_access_object

Check also the sequence diagram here: http://www.oracle.com/technetwork/java/dataaccessobject-138824.html

Maybe a simple example can help you understand the concept:

Let's say we have an entity to represent an employee:

public class Employee {      private int id;     private String name;       public int getId() {         return id;     }     public void setId(int id) {         this.id = id;     }     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     }  } 

The employee entities will be persisted into a corresponding Employee table in a database. A simple DAO interface to handle the database operation required to manipulate an employee entity will be like:

interface EmployeeDAO {      List<Employee> findAll();     List<Employee> findById();     List<Employee> findByName();     boolean insertEmployee(Employee employee);     boolean updateEmployee(Employee employee);     boolean deleteEmployee(Employee employee);  } 

Next we have to provide a concrete implementation for that interface to deal with SQL server, and another to deal with flat files, etc.

like image 124
Rami Avatar answered Oct 04 '22 06:10

Rami


What is DATA ACCESS OBJECT (DAO) -

It is a object/interface, which is used to access data from database of data storage.

WHY WE USE DAO:

To abstract the retrieval of data from a data resource such as a database.
The concept is to "separate a data resource's client interface from its data access mechanism."
 

The problem with accessing data directly is that the source of the data can change. Consider, for example, that your application is deployed in an environment that accesses an Oracle database. Then it is subsequently deployed to an environment that uses Microsoft SQL Server. If your application uses stored procedures and database-specific code (such as generating a number sequence), how do you handle that in your application? You have two options:

  • Rewrite your application to use SQL Server instead of Oracle (or add conditional code to handle the differences), or
  • Create a layer in-between your application logic and data access layers

The DAO Pattern consists of the following:

  • Data Access Object Interface - This interface defines the standard operations to be performed on a model object(s).
  • Data Access Object concrete class -This class implements above interface. This class is responsible to get data from a datasource which can be database / xml or any other storage mechanism.
  • Model Object or Value Object - This object is simple POJO containing get/set methods to store data retrieved using DAO class.

See examples

I hope this has cleared up your understanding of DAO!

like image 31
VeKe Avatar answered Oct 04 '22 05:10

VeKe