diff options
author | Sebastian Schmelzer | 2012-08-24 17:37:11 +0200 |
---|---|---|
committer | Sebastian Schmelzer | 2012-08-24 17:37:11 +0200 |
commit | 06319f98f42581118c2f04d3e66b0bb0acbeea3a (patch) | |
tree | 5dd70691ade9021bb52c5935295b265f3096311d /src | |
parent | add checkstyle, javadoc (diff) | |
download | xss-jpa-06319f98f42581118c2f04d3e66b0bb0acbeea3a.tar.gz xss-jpa-06319f98f42581118c2f04d3e66b0bb0acbeea3a.tar.xz xss-jpa-06319f98f42581118c2f04d3e66b0bb0acbeea3a.zip |
use getter for accessing entityclass
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/nu/xss/jpa/dao/GenericJpaDao.java | 73 | ||||
-rw-r--r-- | src/main/java/nu/xss/jpa/entity/AbstractEntity.java | 11 |
2 files changed, 57 insertions, 27 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; + } + + + } diff --git a/src/main/java/nu/xss/jpa/entity/AbstractEntity.java b/src/main/java/nu/xss/jpa/entity/AbstractEntity.java index c79c17a..29c0022 100644 --- a/src/main/java/nu/xss/jpa/entity/AbstractEntity.java +++ b/src/main/java/nu/xss/jpa/entity/AbstractEntity.java @@ -20,8 +20,6 @@ */ package nu.xss.jpa.entity; -import java.lang.reflect.Field; - import javax.persistence.MappedSuperclass; /** @@ -40,14 +38,7 @@ public abstract class AbstractEntity<K> implements TypedEntity<K> { * Instantiates a new abstract entity. */ public AbstractEntity() { - Field[] f = this.getClass().getFields(); - - System.out.println(f.length); - - for (int i = 0; i < f.length; i++) { - System.out.println(f[i].toGenericString()); - System.out.println(f[i].toString()); - } + // NOOP } /** |