br.com.jhonsapp.bootstrap.object.persistence.generic.AbstractGenericDAO Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bootstrap-object Show documentation
Show all versions of bootstrap-object Show documentation
A complete architecture for creating and managing users.
package br.com.jhonsapp.bootstrap.object.persistence.generic;
import java.util.List;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import br.com.jhonsapp.bootstrap.object.domain.generic.DomainObject;
import br.com.jhonsapp.bootstrap.object.persistence.parameter.Parameter;
public class AbstractGenericDAO implements GenericDAO {
private final static String UNIT_NAME = "PERSISTENCE-UNIT";
@PersistenceContext(unitName = UNIT_NAME)
private EntityManager entityManager;
private Class entityClass;
public AbstractGenericDAO(Class entityClass) {
this.entityClass = entityClass;
}
@Override
public boolean persist(T object) {
try {
entityManager.persist(object);
return true;
} catch (EntityExistsException e) {
return false;
}
}
@Override
public T update(T object) {
return entityManager.merge(object);
}
@Override
public boolean remove(T object) {
if (object.getId() > 0L) {
entityManager.remove(update(object));
return true;
} else {
return false;
}
}
@Override
public T findById(long id) {
if (id <= 0L) {
throw new RuntimeException("The id can not be less than or equal to zero.");
}
return (T) entityManager.find(this.entityClass, id);
}
/**
* This generic method was designed to create a TypedQuery and return it
* there.
*
* @param namedQuery
* the query that will be executed.
* @param parameter
* a set of keys and values as a parameter that will be used in
* the query.
* @return an object that represents a TypedQuery.
*/
private TypedQuery> generateNamedQuery(String namedQuery, Parameter parameter, Class> returnType) {
TypedQuery> typedQuery = entityManager.createNamedQuery(namedQuery, returnType);
if (parameter != null) {
for (String key : parameter.getKeySet()) {
if (key != null) {
typedQuery = typedQuery.setParameter(key, parameter.get(key));
}
}
}
return typedQuery;
}
/**
* This generic method was designed to perform queries that return a single
* result.
*
* @param namedQuery
* the query that will be executed.
* @param parameter
* a set of keys and values as a parameter that will be used in
* the query.
* @return an object or null if the query does not return results.
**/
@SuppressWarnings("unchecked")
protected T find(String namedQuery, Parameter parameter) {
T entity = null;
try {
entity = (T) this.generateNamedQuery(namedQuery, parameter, this.entityClass).getSingleResult();
return entity;
} catch (NoResultException exception) {
return entity;
}
}
/**
* This generic method was designed to perform queries that return objects
* of the primitive type.
*
* @param namedQuery
* the query that will be executed.
* @return objects of the primitive type.
*/
@SuppressWarnings("unchecked")
protected X find(String namedQuery, Parameter parameter, Class returnType) {
X value = null;
try {
value = (X) this.generateNamedQuery(namedQuery, parameter, returnType).getSingleResult();
return value;
} catch (NoResultException exception) {
return value;
}
}
/**
* This generic method was designed to perform queries that return a set of
* objects.
*
* @param namedQuery
* the query that will be executed.
* @return a set of objects or null if the query does not return results.
*/
@SuppressWarnings("unchecked")
protected List findList(String namedQuery, Parameter parameter) {
List entities = null;
try {
entities = (List) this.generateNamedQuery(namedQuery, parameter, this.entityClass).getResultList();
return entities;
} catch (NoResultException exception) {
return entities;
}
}
/**
* This generic method was designed to perform queries that return a set of
* objects.
*
* @param namedQuery
* the query that will be executed.
* @param startPosition
* the position of the first result.
* @param maxResult
* the maximum number of results to retrieve.
* @return a set of objects or null if the query does not return results.
**/
@SuppressWarnings("unchecked")
protected List findList(String namedQuery, Parameter parameter, int startPosition, int maxResult) {
List entities = null;
try {
entities = (List) this.generateNamedQuery(namedQuery, parameter, this.entityClass)
.setFirstResult(startPosition).setMaxResults(maxResult).getResultList();
return entities;
} catch (NoResultException exception) {
return entities;
}
}
/**
* This generic method was designed to perform native queries that return a
* set of objects.
*
* @param sql
* the query that will be executed.
*
* @return a list of objects that satisfy the query.
*/
@SuppressWarnings("unchecked")
protected List findList(String sql) {
return entityManager.createNativeQuery(sql, this.entityClass).getResultList();
}
}