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...?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With