Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DAO and JDBC relation?

I know that Hibernate implements ORM (Object Relational Mapping), what type of mapping does JDBC implement? Does it implement DAO? I don't totally understand how/if DAO is related to JDBC...?

like image 235
rrazd Avatar asked Aug 15 '11 20:08

rrazd


People also ask

What is DAO in Java?

The Data Access Object (or DAO) pattern: separates a data resource's client interface from its data access mechanisms. adapts a specific data resource's access API to a generic client interface.

What is DAO and bean in Java?

DAOs are a pattern concept (http://www.oracle.com/technetwork/java/dataaccessobject-138824.html). Spring Beans are class instances managed by Spring. Of course you can use Spring IOC to implement an application using DAOs.

Is DAO same as ORM?

ORM and DAO are orthogonal concepts. One has to do with how objects are mapped to database tables, the other is a design pattern for writing objects that access data. You don't choose 'between' them. You can have ORM and DAO is the same application, just as you don't need ORM to use the DAO pattern.

Is hibernate a DAO?

Hibernate as you probably know is a object-relational mapping framework implemented using Java language. A DAO can be implemented using Hibernate or even JDBC. Here is an example of an abstract DAO using Hibernate- http://www.java2s.com/Code/Java/Hibernate/GenericDaoCreate.htm.


1 Answers

DAO isn't a mapping. DAO stands for Data Access Object. It look something like this:

public interface UserDAO {

    public User find(Long id) throws DAOException;

    public void save(User user) throws DAOException;

    public void delete(User user) throws DAOException;

    // ...
}

For DAO, JDBC is just an implementation detail.

public class UserDAOJDBC implements UserDAO {

    public User find(Long id) throws DAOException {
        // Write JDBC code here to return User by id.
    }

    // ...
}

Hibernate could be another one.

public class UserDAOHibernate implements UserDAO {

    public User find(Long id) throws DAOException {
        // Write Hibernate code here to return User by id.
    }

    // ...
}

JPA could be another one (in case you're migrating an existing legacy app to JPA; for new apps, it would be a bit weird as JPA is by itself actually the DAO, with e.g. Hibernate and EclipseLink as available implementations).

public class UserDAOJPA implements UserDAO {

    public User find(Long id) throws DAOException {
        // Write JPA code here to return User by id.
    }

    // ...
}

It allows you for switching of UserDAO implementation without changing the business code which is using the DAO (only if you're properly coding against the interface, of course).

For JDBC you'll only need to write a lot of lines to find/save/delete the desired information while with Hibernate it's a matter of only a few lines. Hiberenate as being an ORM takes exactly that nasty JDBC work from your hands, regardless of whether you're using a DAO or not.

See also:

  • I found JPA, or alike, don't encourage DAO pattern
  • JSF Controller, Service and DAO
like image 161
BalusC Avatar answered Oct 04 '22 06:10

BalusC