DAO pattern in "Simple startup for EJBs with Hibernate"
我总觉得“DAO pattern”在这里是多余的,SessionBean是不是可以自己通过Hibernate Session对VO操作?引入DAO有意义吗?迷惑中
Hibernate本质上是一种JDBC级别的产品。
有了CMP就不需要DAO,那是不是有了Hibernate就也不需要DAO了呢?如果下面是纯JDBC,那么DAO很有必要,但是Hibernate已经做了足够的封装,可以实现数据库平台无关,再封一层无非就是可以将来换别的O/R映射工具,我还是觉得意义不大。折中一下,我认为在不太确定通过文件还是数据库实现持久化的时候(或者需要同时支持文件和数据库持久化的时候)使用DAO比较好。谬误之处,还请指教!
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.