DAO pattern in "Simple startup for EJBs with Hibernate"

Simple startup for EJBs with Hibernate
我总觉得“DAO pattern”在这里是多余的,SessionBean是不是可以自己通过Hibernate Session对VO操作?引入DAO有意义吗?迷惑中

DAO是对基于JDBC所有应用的一个桥梁,直接使用Hibernate也可以,但是使用DAO这个中间件后,解耦了Session Bean和JDBC应用联系,这样,你在性能要求情况下,可以换用JDBC代替Hibernate来实现查询等操作。

Hibernate本质上是一种JDBC级别的产品。

正如banq所说,decouple the relation between SB and JDBC.
这样对SB来说,它就是一个client, 它不需要也不应该知道Hibernate的各种opensession, session.save, update等明细操作。封在DAO的一个方法中,也就是抽象出一个大的接口, 以后Hibernate内部操作想变动对SB根本就没有冲击, 另外,Hibernate也有很多O/R Mapping的弱点,如批量删除,更新。我们可以直接换成JDBC的方法,封在DAO方法中,这样对SB永远都是透明的。其实好处也就是分层的思想。

谢谢二位!后来我又仔细读了原文和DAO模式的介绍,感觉还是比较困惑,下面一段摘自Core J2EE Patterns: DataAccessObject pattern
Not Useful for Container-Managed Persistence
Because the EJB container manages entity beans with container-managed persistence (CMP), the container automatically services all persistent storage access. Applications using container-managed entity beans do not need a DAO layer, since the application server transparently provides this functionality. However, DAOs are still useful when a combination of CMP (for entity beans) and BMP (for session beans, servlets) is required.

有了CMP就不需要DAO,那是不是有了Hibernate就也不需要DAO了呢?如果下面是纯JDBC,那么DAO很有必要,但是Hibernate已经做了足够的封装,可以实现数据库平台无关,再封一层无非就是可以将来换别的O/R映射工具,我还是觉得意义不大。折中一下,我认为在不太确定通过文件还是数据库实现持久化的时候(或者需要同时支持文件和数据库持久化的时候)使用DAO比较好。谬误之处,还请指教!

我也有这样的困惑,关于SessionBean ==> Hibernate这样的架构,希望有高手能深入解剖一下。

CMP is persistance layer. The triditional well-known pattern used for j2ee applications is:
(jsp/servlet/pojos...)+ Business Delegator +Session Facade+ CMP/BMP + DB
here CMP/BMP is used for persistance.
As mentioned in Core J2ee patterns, if u change the persistance to DAO pattern. the framework is changed to:
(jsp/servlet/pojos...)+ Business Delegator +(Session Facade)+ DAO+ DB
Here u use JDBC inside DAO to deal the db operations.

about your question "有了CMP就不需要DAO,那是不是有了Hibernate就也不需要DAO了呢?"

The answer is "NO".

Why?
DAO could be a replacement of CMP. Hibernate is also a replacement of CMP but not a replacement of DAO.

For a CMP object, u have methods like create,remove, update, finder...
For DAO, u also have similiar methods like CMP does: create,remove, update, finder...

For Hibernate persistance object, u don't have methods like:create,remove, update, finder... . Instead, u have these operations in your domain objects(or session facade to be consistent with pervious framework). Therefore, u don't need DAO anymore at this point.

Let's go back to the origial topic. Why do we need CMP or DAO? the reason for that is to have a pluggable persistance layer(PPL) and a (PDB)pluggable DB. with PPL, u can easily change your PPL to another PPL(e.g. from CMP to DAO+JDBC, or from CMP to JDO, or to Hibernate). with PDB, u can easily move from Mysql to Oracle...

If u use Hibernate, u will have a pluggable DB automaticly(u can easily move from mssql to oracle). If u wanna have a pluggable persistance(e.g. move from Hibernate to JDO), u need need to implement it in DAO pattern.

Here is another question, do u really need a pluggable persistance layer?
I can't give an exact answer on this question. If u trust Hibernate and u are sure u won't change it to JDO or CMP in the furture, u don't need to have DAO. If u are considering change your persistance someday, it's better to have DAO.