简体   繁体   中英

difference between DAO and hibernate

My tutor told me there is difference between DAO and hibernate...i Have been searching for the answer since yesterday, but couldn't find it. Can anyone explain it clearly please.

DAO (Data Access Object) is a design pattern which defines a way to hide the persistence layer of your application. The classes using its interface will be unaware of the persistence operations and, uncoupled from the database or any other persistece mechanisms you use.

Hibernate is a ORM (object-relational mapping) library, which enable you to map your Java classes to relational tables. Using Hibernate, you can save and query your data from tables without writing SQL queries by hand.

So even if you use Hibernate, you still may want to implement DAO pattern to decouple the persistence layer from your application.

its like the difference between the concept of transport and a diesel engine

DAO is a pattern where you isolate persistence related operations from the rest of your application. its a way to design a data handling component

Hibernate is a specific implementation of an ORM framework. its a tool used to make your DAO implementation simpler (most of the times :P)

They are different things. So you can't compare.

DAO (Data access object) is a design pattern which helps you to centralize and decouple your data access layer.

public abstract MyBaseDAO{
  //Connection settings, common behavior.
}
    
public interface CustomerPersistence{
  //Defines the API
  Customer saveCustomer(Customer customer);
}

public class CustomerPersistenceImpl extends MyBaseDAO implements CustomerPersistence{
  //Implements the API
  public Customer saveCustomer(Customer customer){
    //Use some methods from MyBaseDao
    this.saveObject(customer)
  }
}

This is just a small example. The main idea is that your business logic should only use CustomerPersistence (which remains over the time). MyBaseDAO and CustomerPersistenceImpl could change, but is decoupled from your business.

Regarding Hibernate, you can have CustomerPersistenceHibernateImpl to implement your DAO or another ORM framework without any further changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM