org.yestech.lib.jpa.EntityManagerAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yeslib Show documentation
Show all versions of yeslib Show documentation
A collection of classes that can be used across yestech artifacts/components, but must not be dependant
on any yestech component. Most of the code is utility type code. When more than a few classes are
found to be in a package or the package start to handle more that a few reposibilities then a new
independant component is created and the existing code in yeslib is ported to the new component.
/*
* Copyright LGPL3
* YES Technology Association
* http://yestech.org
*
* http://www.opensource.org/licenses/lgpl-3.0.html
*/
/*
*
* Author: Artie Copeland
* Last Modified Date: $DateTime: $
*/
package org.yestech.lib.jpa;
import static org.yestech.lib.util.CastUtil.cast;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
/**
* A adapter for a JPA {@link javax.persistence.EntityManager}.
*
* @author Artie Copeland
* @version $Revision: $
*/
public class EntityManagerAdapter {
@PersistenceContext
private EntityManager entityManager;
public EntityManagerAdapter() {
}
/**
* Defines ASC and DESC sort orders for queries.
*/
public enum SortOrder {
ASC, DESC
}
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
public T findById(final Class entityClass, final Object id) {
if (null == entityClass) throw new IllegalArgumentException("entityClass can't be null");
if (null == id) throw new IllegalArgumentException("id can't be null");
return entityManager.find(entityClass, id);
}
public boolean delete(final Object entity) {
if (null == entity) throw new IllegalArgumentException("entity can't be null");
entityManager.remove(entity);
return true;
}
public boolean deleteById(final Class entityClass, final Object id) {
if (null == entityClass) throw new IllegalArgumentException("entityClass can't be null");
if (null == id) throw new IllegalArgumentException("id can't be null");
return delete(findById(entityClass, id));
}
public List getAllEntities(final Class entityClass) {
if (null == entityClass) throw new IllegalArgumentException("entityClass can't be null");
return cast(entityManager.createQuery("select e from " + entityClass.getSimpleName() + " e")
.getResultList());
}
public List getAllEntities(final Class entityClass, final String orderByAttributeName,
final SortOrder sortOrder) {
if (null == entityClass) throw new IllegalArgumentException("entityClass can't be null");
if (null == orderByAttributeName) throw new IllegalArgumentException("orderByAttributeName can't be null");
return cast(entityManager.createQuery(
"select e from " + entityClass.getSimpleName() + " e order by e." + orderByAttributeName + " "
+ sortOrder.name()).getResultList());
}
/**
* Checks to see if the entity exists. if it foes then merge, else persist.
*
* @param entityClass
* @param entity
* @param
* @return
*/
public T persistOrMerge(final Class entityClass, final T entity) {
if (null == entityClass) throw new IllegalArgumentException("entityClass can't be null");
if (null == entity) throw new IllegalArgumentException("entity can't be null");
if (!entityManager.contains(entity)) {
persist(entityClass, entity);
} else {
merge(entityClass, entity);
}
return entity;
}
public T persist(final Class entityClass, final T entity) {
if (null == entityClass) throw new IllegalArgumentException("entityClass can't be null");
if (null == entity) throw new IllegalArgumentException("entity can't be null");
entityManager.persist(entity);
return entity;
}
public T merge(final Class entityClass, final T entity) {
if (null == entityClass) throw new IllegalArgumentException("entityClass can't be null");
if (null == entity) throw new IllegalArgumentException("entity can't be null");
return entityManager.merge(entity);
}
public long countEntities(final Class> entityClass) {
if (null == entityClass) throw new IllegalArgumentException("entityClass can't be null");
return (Long) entityManager.createQuery("select count(entity) from " + entityClass.getSimpleName() + " entity")
.getSingleResult();
}
public long countEntitiesByAttribute(final Class> entityClass, final String attributeName,
final Object attributeValue) {
if (null == entityClass) throw new IllegalArgumentException("entityClass can't be null");
if (null == attributeName) throw new IllegalArgumentException("attributeName can't be null");
if (null == attributeValue) throw new IllegalArgumentException("attributeValue can't be null");
return (Long) entityManager.createQuery(
"select count(e) from " + entityClass.getSimpleName() + " e where e." + attributeName + " = ?1").setParameter(
1, attributeValue).getSingleResult();
}
}