-
Notifications
You must be signed in to change notification settings - Fork 160
/
EntityManager.java
269 lines (241 loc) · 7.71 KB
/
EntityManager.java
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*******************************************************************************
* Copyright (c) 2011, 2017 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.commons.jpa;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.model.id.KapuaEid;
import org.eclipse.kapua.model.KapuaEntity;
import org.eclipse.kapua.model.id.KapuaId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.persistence.LockModeType;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import java.io.Serializable;
/**
* Kapua JPA entity manager wrapper
*
* @since 1.0
*/
public class EntityManager {
private static final Logger LOG = LoggerFactory.getLogger(AbstractEntityManagerFactory.class);
private javax.persistence.EntityManager javaxPersitenceEntityManager;
/**
* Constructs a new entity manager wrapping the given {@link javax.persistence.EntityManager}
*
* @param javaxPersitenceEntityManager
*/
public EntityManager(javax.persistence.EntityManager javaxPersitenceEntityManager) {
this.javaxPersitenceEntityManager = javaxPersitenceEntityManager;
}
/**
* Opens a Jpa Transaction.
*
* @throws KapuaException if {@link org.eclipse.kapua.commons.jpa.EntityManager} is {@code null}
*/
public void beginTransaction()
throws KapuaException {
if (javaxPersitenceEntityManager == null) {
throw KapuaException.internalError(new NullPointerException(), "null EntityManager");
}
javaxPersitenceEntityManager.getTransaction().begin();
}
/**
* Commits the current Jpa Transaction.
*
* @throws KapuaException
*/
public void commit()
throws KapuaException {
if (javaxPersitenceEntityManager == null) {
throw KapuaException.internalError("null EntityManager");
}
if (!javaxPersitenceEntityManager.getTransaction().isActive()) {
throw KapuaException.internalError("Transaction Not Active");
}
try {
javaxPersitenceEntityManager.getTransaction().commit();
} catch (Exception e) {
throw KapuaException.internalError(e, "Commit Error");
}
}
/**
* Rollbacks the current Jpa Transaction. No exception will be thrown when rolling back so that the original exception that caused the rollback can be thrown.
*/
public void rollback() {
try {
if (javaxPersitenceEntityManager != null &&
javaxPersitenceEntityManager.getTransaction().isActive()) {
javaxPersitenceEntityManager.getTransaction().rollback();
}
} catch (Exception e) {
LOG.warn("Rollback Error", e);
}
}
/**
* Return the transaction status
*
* @return
*/
public boolean isTransactionActive() {
return (javaxPersitenceEntityManager != null &&
javaxPersitenceEntityManager.getTransaction().isActive());
}
/**
* Closes the EntityManager
*/
public void close() {
if (javaxPersitenceEntityManager != null) {
javaxPersitenceEntityManager.close();
}
}
/**
* Persist a {@link Serializable} entity;
*
* @param entity
*/
public <E extends Serializable> void persist(E entity) {
javaxPersitenceEntityManager.persist(entity);
}
/**
* Persist a {@link KapuaEntity}
*
* @param entity
* @since 1.0.0
*/
public <E extends KapuaEntity> void persist(E entity) {
persist((Serializable) entity);
}
/**
* Flush the {@link EntityManager}
*
* @since 1.0.0}
*/
public void flush() {
javaxPersitenceEntityManager.flush();
}
/**
* Finds a {@link KapuaEntity} by the given id and type
*
* @param clazz The {@link Serializable} entity type.
* @param id The id of the entity.
* @return The found {@link Serializable} or {@code null} otherwise.
* @since 1.2.0
*/
public <E extends Serializable> E find(Class<E> clazz, Object id) {
return javaxPersitenceEntityManager.find(clazz, id);
}
/**
* Finds a {@link KapuaEntity} by the given id and type
*
* @param clazz The {@link KapuaEntity} type.
* @param id The {@link KapuaEntity#getId()}
* @return The found {@link KapuaEntity} or {@code null} otherwise.
* @since 1.0.0
*/
public <E extends KapuaEntity> E find(Class<E> clazz, KapuaId id) {
KapuaEid eid = KapuaEid.parseKapuaId(id);
return javaxPersitenceEntityManager.find(clazz, eid);
}
/**
* Find a {@link Serializable} by the given id and type
*
* @param clazz
* @param id
* @return
* @since 1.0.0
*/
public <E extends Serializable> E findWithLock(Class<E> clazz, Object id) {
return javaxPersitenceEntityManager.find(clazz, id, LockModeType.PESSIMISTIC_WRITE);
}
/**
* Merges a {@link Serializable} entity.
*
* @param entity The {@link Serializable} entity to merge.
* @since 1.2.0.
*/
public <E extends Serializable> void merge(E entity) {
javaxPersitenceEntityManager.merge(entity);
}
/**
* Merges a {@link KapuaEntity}
*
* @param entity The {@link KapuaEntity} to merge.
* @since 1.0.0.
*/
public <E extends KapuaEntity> void merge(E entity) {
merge((Serializable) entity);
}
/**
* Refresh a {@link Serializable} entity
*
* @param entity The {@link Serializable} entity to refresh.
* @since 1.2.0
*/
public <E extends Serializable> void refresh(E entity) {
javaxPersitenceEntityManager.refresh(entity);
}
/**
* Refresh a {@link KapuaEntity}
*
* @param entity The {@link KapuaEntity} to refresh.
* @since 1.0.0
*/
public <E extends KapuaEntity> void refresh(E entity) {
refresh((Serializable) entity);
}
/**
* Remove the entity
*
* @param entity
*/
public <E extends KapuaEntity> void remove(E entity) {
javaxPersitenceEntityManager.remove(entity);
}
/**
* Return the {@link javax.persistence.criteria.CriteriaBuilder}
*
* @return
*/
public CriteriaBuilder getCriteriaBuilder() {
return javaxPersitenceEntityManager.getCriteriaBuilder();
}
/**
* Return the typed query based on the criteria
*
* @param criteriaSelectQuery
* @return
*/
public <E> TypedQuery<E> createQuery(CriteriaQuery<E> criteriaSelectQuery) {
return javaxPersitenceEntityManager.createQuery(criteriaSelectQuery);
}
/**
* Return the typed query based on the query name
*
* @param queryName
* @param clazz
* @return
*/
public <E> TypedQuery<E> createNamedQuery(String queryName, Class<E> clazz) {
return javaxPersitenceEntityManager.createNamedQuery(queryName, clazz);
}
/**
* Return native query based on provided sql query
*
* @param querySelectUuidShort
* @return
*/
public <E> Query createNativeQuery(String querySelectUuidShort) {
return javaxPersitenceEntityManager.createNativeQuery(querySelectUuidShort);
}
}