Commit 5a166841528cae6c07e81a5003dfdd7bffb84bce
1 parent
78b1332d
Fix EntityManager injection in DAO implementation
Showing
3 changed files
with
45 additions
and
60 deletions
services/Global/src/main/java/de/bht/beuthbot/daos/AppUserDAO.java
@@ -10,11 +10,7 @@ import javax.ejb.Stateless; | @@ -10,11 +10,7 @@ import javax.ejb.Stateless; | ||
10 | * Created by Benjamin Rühl on 19.12.2017. | 10 | * Created by Benjamin Rühl on 19.12.2017. |
11 | */ | 11 | */ |
12 | @Stateless | 12 | @Stateless |
13 | -public class AppUserDAO extends GenericHibernateDAO<User, Long> implements UserDAO { | ||
14 | - | ||
15 | - public AppUserDAO() { | ||
16 | - setEntityClass(AppUser.class); | ||
17 | - } | 13 | +public class AppUserDAO extends GenericHibernateDAO<User, AppUser, Long> implements UserDAO { |
18 | 14 | ||
19 | @Override | 15 | @Override |
20 | public User createUser() { | 16 | public User createUser() { |
services/Global/src/main/java/de/bht/beuthbot/daos/GenericHibernateDAO.java
@@ -2,102 +2,91 @@ package de.bht.beuthbot.daos; | @@ -2,102 +2,91 @@ package de.bht.beuthbot.daos; | ||
2 | 2 | ||
3 | import de.bht.beuthbot.dataAccess.GenericDAO; | 3 | import de.bht.beuthbot.dataAccess.GenericDAO; |
4 | import de.bht.beuthbot.model.entities.Entity; | 4 | import de.bht.beuthbot.model.entities.Entity; |
5 | -import org.hibernate.Criteria; | ||
6 | import org.hibernate.Session; | 5 | import org.hibernate.Session; |
7 | -import org.hibernate.criterion.Criterion; | ||
8 | 6 | ||
7 | +import javax.annotation.PostConstruct; | ||
9 | import javax.ejb.Stateless; | 8 | import javax.ejb.Stateless; |
10 | -import javax.enterprise.inject.InjectionException; | ||
11 | import javax.persistence.EntityManager; | 9 | import javax.persistence.EntityManager; |
12 | import javax.persistence.EntityManagerFactory; | 10 | import javax.persistence.EntityManagerFactory; |
13 | import javax.persistence.PersistenceContext; | 11 | import javax.persistence.PersistenceContext; |
14 | import javax.persistence.PersistenceUnit; | 12 | import javax.persistence.PersistenceUnit; |
13 | +import javax.persistence.criteria.CriteriaBuilder; | ||
14 | +import javax.persistence.criteria.CriteriaQuery; | ||
15 | +import javax.persistence.criteria.Predicate; | ||
16 | +import javax.persistence.criteria.Root; | ||
15 | import java.io.Serializable; | 17 | import java.io.Serializable; |
16 | import java.lang.reflect.ParameterizedType; | 18 | import java.lang.reflect.ParameterizedType; |
17 | import java.util.List; | 19 | import java.util.List; |
18 | 20 | ||
19 | /** | 21 | /** |
20 | * Created by Benjamin Rühl on 19.12.2017. | 22 | * Created by Benjamin Rühl on 19.12.2017. |
23 | + * Base class for DAO implementations using hibernate for persistence. | ||
24 | + * Provides generic functionality for interaction with entities. | ||
25 | + * @param <I> Interface type of entity class | ||
26 | + * @param <T> Implementation type of entity class | ||
27 | + * @param <ID> Type of entity's primary id | ||
21 | */ | 28 | */ |
22 | @Stateless | 29 | @Stateless |
23 | -public class GenericHibernateDAO<T extends Entity, ID extends Serializable> implements GenericDAO<T, ID> { | 30 | +public class GenericHibernateDAO<I extends Entity, T extends I, ID extends Serializable> implements GenericDAO<I, ID> { |
24 | 31 | ||
25 | - private Class<? extends T> entityClass; | ||
26 | - private Session session; | 32 | + private Class<T> entityClass; |
27 | 33 | ||
28 | - @PersistenceUnit(unitName = "PostgreSQLDS") | ||
29 | - private EntityManagerFactory emf; | 34 | + @PersistenceUnit(unitName = "PostgresPU") |
35 | + private EntityManagerFactory entityManagerFactory; | ||
30 | 36 | ||
31 | - @PersistenceContext(unitName = "PostgreSQLDS") | ||
32 | - private EntityManager em; | 37 | + @PersistenceContext(unitName = "PostgresPU") |
38 | + private EntityManager entityManager; | ||
33 | 39 | ||
34 | @SuppressWarnings("unchecked cast") | 40 | @SuppressWarnings("unchecked cast") |
35 | - public GenericHibernateDAO() { | ||
36 | - if (em == null) | 41 | + @PostConstruct |
42 | + public void init() { | ||
43 | + if (entityManager == null) | ||
37 | throw new RuntimeException("EntityManager must not be null"); | 44 | throw new RuntimeException("EntityManager must not be null"); |
38 | 45 | ||
39 | - entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; | ||
40 | - setSession( (Session)em.getDelegate() ); | 46 | + entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1]; |
41 | } | 47 | } |
42 | 48 | ||
43 | - public void setSession(Session s) { | ||
44 | - this.session = s; | ||
45 | - } | ||
46 | - | ||
47 | - protected Session getSession() { | ||
48 | - if (session == null) | ||
49 | - throw new IllegalStateException("Session has not been set on DAO before usage"); | ||
50 | - return session; | ||
51 | - } | ||
52 | - | ||
53 | - /** | ||
54 | - * Use this in subclasses to set entityClass to derived type of T | ||
55 | - */ | ||
56 | - protected void setEntityClass(Class<? extends T> entityClass) { | ||
57 | - this.entityClass = entityClass; | ||
58 | - } | ||
59 | - | ||
60 | - public Class<? extends T> getEntityClass() { | 49 | + public Class<T> getEntityClass() { |
61 | return entityClass; | 50 | return entityClass; |
62 | } | 51 | } |
63 | 52 | ||
64 | @Override | 53 | @Override |
65 | - public T findById(ID id) { | ||
66 | - return getSession().load(getEntityClass(), id); | 54 | + public I findById(ID id) { |
55 | + return entityManager.find(getEntityClass(), id); | ||
67 | } | 56 | } |
68 | 57 | ||
69 | - public List<T> findAll() { | 58 | + @Override |
59 | + public List<I> findAll() { | ||
70 | return findByCriteria(); | 60 | return findByCriteria(); |
71 | } | 61 | } |
72 | 62 | ||
73 | @Override | 63 | @Override |
74 | - public T saveOrUpdate(T entity) { | ||
75 | - getSession().saveOrUpdate(entity); | 64 | + public I saveOrUpdate(I entity) { |
65 | + entityManager.merge(entity); | ||
76 | return entity; | 66 | return entity; |
77 | } | 67 | } |
78 | 68 | ||
79 | @Override | 69 | @Override |
80 | - public void delete(T entity) { | ||
81 | - getSession().delete(entity); | ||
82 | - } | ||
83 | - | ||
84 | - public void flush() { | ||
85 | - getSession().flush(); | ||
86 | - } | ||
87 | - | ||
88 | - public void clear() { | ||
89 | - getSession().clear(); | 70 | + public void delete(I entity) { |
71 | + entityManager.remove(entity); | ||
90 | } | 72 | } |
91 | 73 | ||
92 | /** | 74 | /** |
93 | * Use this inside subclasses as a convenience method. | 75 | * Use this inside subclasses as a convenience method. |
94 | */ | 76 | */ |
95 | - @SuppressWarnings("unchecked") | ||
96 | - protected List<T> findByCriteria(Criterion... criterion) { | ||
97 | - Criteria crit = getSession().createCriteria(getEntityClass()); | ||
98 | - for (Criterion c : criterion) { | ||
99 | - crit.add(c); | ||
100 | - } | ||
101 | - return crit.list(); | 77 | + protected List<I> findByCriteria(Predicate... restrictions) { |
78 | + CriteriaBuilder builder = entityManager.getCriteriaBuilder(); | ||
79 | + CriteriaQuery<T> criteria = builder.createQuery(getEntityClass()); | ||
80 | + Root<T> criteriaRoot = criteria.from(getEntityClass()); | ||
81 | + | ||
82 | + criteria.select(criteriaRoot); | ||
83 | + criteria.where(restrictions); | ||
84 | + | ||
85 | + List<T> elements = entityManager.createQuery(criteria).getResultList(); | ||
86 | + return (List<I>) elements; | ||
87 | + } | ||
88 | + | ||
89 | + protected EntityManager getEntityManager() { | ||
90 | + return entityManager; | ||
102 | } | 91 | } |
103 | } | 92 | } |
services/Global/src/main/resources/META-INF/persistence.xml
@@ -4,7 +4,7 @@ | @@ -4,7 +4,7 @@ | ||
4 | http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" | 4 | http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" |
5 | version="2.1"> | 5 | version="2.1"> |
6 | 6 | ||
7 | - <persistence-unit name="PostgreSQLDS" transaction-type="JTA"> | 7 | + <persistence-unit name="PostgresPU" transaction-type="JTA"> |
8 | 8 | ||
9 | <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | 9 | <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> |
10 | 10 |