Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any good generic JPA DAO implemenation?

Tags:

java

jpa

dao

According this article, generic JPA DAO(Data Access Object) is a pretty nice pattern.

Is there any good implementation?

like image 420
hoymkot Avatar asked Jun 15 '12 22:06

hoymkot


People also ask

What is generic DAO?

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.

Is Spring data JPA better than Hibernate?

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.

Can spring HibernateTemplate simplify your DAO implementation by managing sessions and transactions for you?

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.


2 Answers

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.

like image 151
Francisco Spaeth Avatar answered Oct 05 '22 23:10

Francisco Spaeth


Just wanted to mention a couple more generic dao implementations for JPA:

  • Generic DAO toolkit - http://code.google.com/p/generic-dao/
  • Related Question in SO - Single DAO & generic CRUD methods (JPA/Hibernate + Spring)
  • A good blog article on JPA DAO - http://blog.xebia.com/2009/03/09/jpa-implementation-patterns-data-access-objects/
like image 45
Biju Kunjummen Avatar answered Oct 06 '22 00:10

Biju Kunjummen