Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
955 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...