According this article, generic JPA DAO(Data Access Object) is a pretty nice pattern.
Is there any good implementation?
General and Generic DAOsThe GeneralDAO is a single DAO class that can be used to access all classes of domain objects. GeneralDAO has methods that require the developer to specify which type of domain object to use. An application only needs a single instance of the GeneralDAO class to access all domain objects.
JPA uses EntityManager interface to create/read/delete operation and maintains the persistence context. Hibernate uses Session interface to create/read/delete operation and maintains the persistence context. JPA uses JPQL (Java Persistence Query Language) as Object Oriented Query language for database operations.
Spring HibernateTemplate can simplify your DAO implementation by managing sessions and transactions for you. Explanation: However, using HibernateTemplate means your DAO has to depend on Spring API. Explanation: An alternative to Spring HibernateTemplate is to use Hibernate contextual sessions.
You could take a look into the Spring Data JPA.
A few new concepts were introduced into Spring Data JPA, for instance the Query creation based on the method name, so you can declare a method like findById(String id)
and the "generic" implementation will interpret the method's name and execute something like select Entity from Entity where id = 'given string'
Methods like findByNameAndLastName(String name, String lastName)
or even findByNameOrInternalId(String name, int internalId)
are supported too.
Just wanted to mention a couple more generic dao implementations for JPA:
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