Created
June 6, 2012 13:18
-
-
Save mcgivrer/2881806 to your computer and use it in GitHub Desktop.
A DAO and its Generic implementation inspired/guided by an Augusto GIST and simplified/adapted (see reference in the first file)to our needs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.mcgivrer.framework.web.persist; | |
import java.io.Serializable; | |
import java.util.List; | |
import java.util.Map; | |
public interface Dao <T, PK extends Serializable> { | |
/** | |
* save an entity. This can be either a INSERT or UPDATE in the database. | |
* | |
* @param entity the entity to save | |
* | |
* @return the saved entity | |
*/ | |
T save(T entity); | |
/** | |
* Find an entity by its primary key | |
* | |
* @param id the primary key | |
* @return the entity | |
*/ | |
T findById(PK id); | |
/** | |
* Load all entities. | |
* | |
* @return the list of entities | |
*/ | |
List<T> findAll(); | |
/** | |
* Find using a named query. | |
* | |
* @param queryName the name of the query | |
* @param params the query parameters | |
* | |
* @return the list of entities | |
*/ | |
List<T> findByNamedQuery(String queryName, Object... params); | |
/** | |
* Find using a named query. | |
* | |
* @param queryName the name of the query | |
* @param params the query parameters | |
* | |
* @return the list of entities | |
*/ | |
List<T> findByNamedQueryAndNamedParams(String queryName, Map<String, ?> params); | |
/** | |
* Count all entities. | |
* | |
* @return the number of entities | |
*/ | |
int countAll(); | |
/** | |
* Merge the state of the given entity into the current persistence context, this will also save the entity. | |
* | |
* @param entity the entity to save | |
* | |
* @return the saved entity | |
*/ | |
T merge(T entity); | |
/** | |
* delete an entity from the database. | |
* | |
* @param entity the entity to delete | |
*/ | |
void delete(T entity); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.mcgivrer.framework.web.persist; | |
import java.io.Serializable; | |
import java.lang.reflect.ParameterizedType; | |
import java.util.List; | |
import java.util.Map; | |
import javax.persistence.EntityManager; | |
import javax.persistence.PersistenceContext; | |
import org.hibernate.Criteria; | |
import org.hibernate.Session; | |
import org.hibernate.criterion.Criterion; | |
import org.hibernate.criterion.Order; | |
import org.hibernate.criterion.Projections; | |
/** | |
* GenericDAO providing base of all database operation on entity T. | |
* | |
* largely inspired by the following GIST: https://gist.github.com/1261256 | |
* Thanks to augusto for providing such a cool implementation. | |
* | |
* @author mcgivrer | |
* | |
* @param <T> | |
* @param <PK> | |
*/ | |
public class DaoGeneric<T extends Serializable, PK extends Serializable> | |
implements Dao<T, PK> { | |
protected Class<T> entityClass; | |
@PersistenceContext(name = "application") | |
protected EntityManager entityManager; | |
public DaoGeneric() { | |
ParameterizedType genericSuperClass = (ParameterizedType) getClass() | |
.getGenericSuperclass(); | |
@SuppressWarnings("unchecked") | |
Class<T> class1 = (Class<T>) genericSuperClass | |
.getActualTypeArguments()[0]; | |
this.entityClass = class1; | |
} | |
@Override | |
public T save(T entity) { | |
this.entityManager.persist(entity); | |
return entity; | |
} | |
@Override | |
public T findById(PK id) { | |
return this.entityManager.find(entityClass, id); | |
} | |
@Override | |
public List<T> findAll() { | |
return findByCriteria(); | |
} | |
@Override | |
public List<T> findByNamedQuery(final String name, Object... params) { | |
javax.persistence.Query query = entityManager.createNamedQuery(name); | |
for (int i = 0; i < params.length; i++) { | |
query.setParameter(i + 1, params[i]); | |
} | |
return (List<T>) query.getResultList(); | |
} | |
@Override | |
public List<T> findByNamedQueryAndNamedParams(final String name, | |
final Map<String, ?> params) { | |
javax.persistence.Query query = entityManager.createNamedQuery(name); | |
for (final Map.Entry<String, ?> param : params.entrySet()) { | |
query.setParameter(param.getKey(), param.getValue()); | |
} | |
return (List<T>) query.getResultList(); | |
} | |
@Override | |
public int countAll() { | |
return countByCriteria(); | |
} | |
@Override | |
public T merge(T entity) { | |
return this.entityManager.merge(entity); | |
} | |
@Override | |
public void delete(T entity) { | |
this.entityManager.remove(entity); | |
} | |
protected List<T> findByCriteria(final Criterion... criterion) { | |
return findByCriteria(-1, -1, null, criterion); | |
} | |
protected List<T> findByCriteria(final int firstResult, | |
final int maxResults, final Order order, | |
final Criterion... criterion) { | |
Session session = (Session) entityManager.getDelegate(); | |
Criteria crit = session.createCriteria(entityClass); | |
for (final Criterion c : criterion) { | |
if (c != null) { | |
crit.add(c); | |
} | |
} | |
if (order != null) { | |
crit.addOrder(order); | |
} | |
if (firstResult > 0) { | |
crit.setFirstResult(firstResult); | |
} | |
if (maxResults > 0) { | |
crit.setMaxResults(maxResults); | |
} | |
return crit.list(); | |
} | |
protected int countByCriteria(Criterion... criterion) { | |
Session session = (Session) entityManager.getDelegate(); | |
Criteria crit = session.createCriteria(entityClass); | |
crit.setProjection(Projections.rowCount()); | |
for (final Criterion c : criterion) { | |
if (c != null) { | |
crit.add(c); | |
} | |
} | |
return ((Long) crit.list().get(0)).intValue(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment