GenericDao.java
3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package net.ziemers.swxercise.db.dao;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.enterprise.inject.Alternative;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import net.ziemers.swxercise.db.BaseEntity;
/**
* Stellt generische Persistenz-Funktionalität zur Verfügung. Wird normalerweise von den
* spazialisierten Data Access Objects geerbt.
*
* Copyright (c) 2017 Dipl.-Inform. Thomas Ziemer
*
* This file is part of swXercise.
*
* swXercise is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* swXercise is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with swXercise.
* If not, see <http://www.gnu.org/licenses/>.
*/
@Stateless
// diese Bean ist nur die Alternative zur Ermittlung des Entity Managers
// Quelle: https://stackoverflow.com/questions/10185976/cdi-ambiguous-dependencies
@Alternative
public class GenericDao {
/*
* Der Entity Manager wird per CDI injiziert, damit er auch im Testkontext mit dem CDI-Runner funktioniert.
* Im JEE-Container "zieht" dann die Produktion des Entity Managers in der Klasse "EntityMangerProducer".
*/
@Inject
protected EntityManager entityManager;
/**
* Speichert die gegebene Entität.
*
* @see EntityManager#persist(Object)
* @param entity die zu speichernde Entität
* @param <T> Generic für den Entity-Typ.
* @return die Id der persistierten Entität
*/
public <T extends BaseEntity> Long save(final T entity) {
entityManager.persist(entity);
return entity.getId();
}
public <T extends BaseEntity> T saveOrUpdate(final T entity) {
return entityManager.merge(entity);
}
public <T extends BaseEntity> T remove(Class<T> entityType, Long primaryKey) {
// In order to delete an object from the database, it has to first be
// retrieved and then in an active transaction, it can be deleted using
// the remove() method.
final T entity = findById(entityType, primaryKey);
// An "IllegalArgumentException" is thrown by remove(), if the argument
// is not an instance of an entity class or if it is a detached entity.
entityManager.remove(entity);
return entity;
}
/**
* @param entityType Entity-Typ.
* @param primaryKey Id
* @param <T> Generic für Entity-Typ
* @return die Entität mit gegebener Id oder <code>null</code>, wenn keine gefunden
*/
public <T extends BaseEntity> T findById(Class<T> entityType, Long primaryKey) {
final T entity = entityManager.find(entityType, primaryKey);
return entity;
}
/**
* @param entityType Entity-Typ.
* @param <T> Generic für den Entity-Typ.
* @return alle Entitäten vom gegebenen Typ
*/
public <T extends BaseEntity> Collection<T> findAll(Class<T> entityType) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<T> cq = cb.createQuery(entityType);
Root<T> root = cq.from(entityType);
CriteriaQuery<T> selectAll = cq.select(root);
TypedQuery<T> query = entityManager.createQuery(selectAll);
return query.getResultList();
}
}