summaryrefslogtreecommitdiffstats
path: root/src/main/java/nu/xss/jpa/dao
diff options
context:
space:
mode:
authorSebastian Schmelzer2012-08-24 17:37:11 +0200
committerSebastian Schmelzer2012-08-24 17:37:11 +0200
commit06319f98f42581118c2f04d3e66b0bb0acbeea3a (patch)
tree5dd70691ade9021bb52c5935295b265f3096311d /src/main/java/nu/xss/jpa/dao
parentadd checkstyle, javadoc (diff)
downloadxss-jpa-06319f98f42581118c2f04d3e66b0bb0acbeea3a.tar.gz
xss-jpa-06319f98f42581118c2f04d3e66b0bb0acbeea3a.tar.xz
xss-jpa-06319f98f42581118c2f04d3e66b0bb0acbeea3a.zip
use getter for accessing entityclass
Diffstat (limited to 'src/main/java/nu/xss/jpa/dao')
-rw-r--r--src/main/java/nu/xss/jpa/dao/GenericJpaDao.java73
1 files changed, 56 insertions, 17 deletions
diff --git a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java
index 20be33f..c079f83 100644
--- a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java
+++ b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java
@@ -60,7 +60,7 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
private static final long serialVersionUID = 4998055731089977476L;
/** The logger. */
- private Logger logger = LoggerFactory.getLogger(this.getClass());
+ private Logger logger = LoggerFactory.getLogger(this.getClass());
/** The entity manager. */
@PersistenceContext
@@ -88,13 +88,13 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
@Override
public void save(final E entity) {
- entityManager.persist(entity);
+ getEntityManager().persist(entity);
logger.info("Saved entity: {}.", entity.toString());
}
@Override
public void merge(final E entity) {
- entityManager.merge(entity);
+ getEntityManager().merge(entity);
logger.info("Updated unmanaged entity: {}.", entity.toString());
}
@@ -102,14 +102,14 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
public void delete(final E entity) {
final E persistentEntity = findById(entity.getId());
if (persistentEntity != null) {
- entityManager.remove(persistentEntity);
+ getEntityManager().remove(persistentEntity);
}
logger.info("Deleted entity: {}.", entity.toString());
}
@Override
public final E findById(final K id) {
- return entityManager.find(entityClass, id);
+ return getEntityManager().find(entityClass, id);
}
@Override
@@ -164,6 +164,17 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
return find(null, sort, pagination, filter);
}
+
+ /**
+ * Creates the query.
+ *
+ * @return the criteria query
+ */
+ protected final CriteriaQuery<E> createQuery() {
+ CriteriaQuery<E> c = getCriteriaBuilder().createQuery(this.entityClass);
+ return c;
+ }
+
/**
* Find.
*
@@ -225,7 +236,7 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
q = createQuery();
}
- root = q.from(this.entityClass);
+ root = q.from(getEntityClass());
q.select(root);
if (filter != null) {
@@ -253,13 +264,13 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
}
// Result pagination
- TypedQuery<E> tq = entityManager.createQuery(q);
+ TypedQuery<E> tq = getEntityManager().createQuery(q);
if (pagination != null) {
if (pagination.getCount() > 0) {
- tq.setFirstResult(pagination.getCount());
+ tq.setMaxResults(pagination.getOffset());
}
if (pagination.getOffset() > 0) {
- tq.setMaxResults(pagination.getOffset());
+ tq.setFirstResult(pagination.getCount());
}
}
@@ -274,7 +285,7 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
* @return the e
*/
protected final E findSingle(final CriteriaQuery<E> query) {
- return entityManager.createQuery(query).getSingleResult();
+ return getEntityManager().createQuery(query).getSingleResult();
}
/**
@@ -320,17 +331,45 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
* @return the criteria builder
*/
protected final CriteriaBuilder getCriteriaBuilder() {
- return entityManager.getCriteriaBuilder();
+ return getEntityManager().getCriteriaBuilder();
}
/**
- * Creates the query.
- *
- * @return the criteria query
+ * Gets the logger.
+ *
+ * @return the logger
*/
- protected final CriteriaQuery<E> createQuery() {
- CriteriaQuery<E> c = getCriteriaBuilder().createQuery(this.entityClass);
- return c;
+ public final Logger getLogger() {
+ return logger;
}
+ /**
+ * Gets the entity manager.
+ *
+ * @return the entity manager
+ */
+ public final EntityManager getEntityManager() {
+ return entityManager;
+ }
+
+ /**
+ * Sets the entity manager.
+ *
+ * @param entityManager the new entity manager
+ */
+ public final void setEntityManager(final EntityManager entityManager) {
+ this.entityManager = entityManager;
+ }
+
+ /**
+ * Gets the entity class.
+ *
+ * @return the entity class
+ */
+ public Class<E> getEntityClass() {
+ return entityClass;
+ }
+
+
+
}