Generic Data Access Objects [转]
The DAO interfaces An implementation with Hibernate Preparing DAOs with factories Preparing DAOs with manual dependency injection Preparing DAOs with lookup Writing DAOs as managed EJB 3.0 components A Better typed Generic DAO? You decide!
- The DAO interfaces
- An implementation with Hibernate
- Preparing DAOs with factories
- Preparing DAOs with manual dependency injection
- Preparing DAOs with lookup
- Writing DAOs as managed EJB 3.0 components
- A Better typed Generic DAO? You decide!
This is a pattern for Data Access Objects with JDK 5.0, from the CaveatEmptor example application. It is also explained in the book Java Persistence with Hibernate. Two links you might find useful: Sessions and transactions and Open Session in View.
This time I based the DAO example on interfaces. Tools like Hibernate already provide database portability, so persistence layer portability shouldn't be a driving motivation for interfaces. However, DAO interfaces make sense in more complex applications, when several persistence services are encapsulate in one persistence layer. I'd say that you should use Hibernate (or Java Persistence APIs) directly in most cases, the best reason to use an additional DAO layer is higher abstraction (e.g. methods like getMaximumBid()instead of session.createQuery(...) repeated a dozen times).
The DAO interfaces
I use one interface per persistent entity, with a super interface for common CRUD functionality:
public interface GenericDAOextends
上一篇: PHP数组的四个TIPS