/
JPA.java
185 lines (144 loc) · 4.15 KB
/
JPA.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
package org.rapidoid.jpa;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.ctx.Ctx;
import org.rapidoid.ctx.Ctxs;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.metamodel.EntityType;
import java.util.List;
import java.util.Map;
/*
* #%L
* rapidoid-commons
* %%
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
@Authors("Nikolche Mihajlovski")
@Since("5.1.0")
public class JPA {
public static EntityManager em() {
return JPAUtil.em();
}
public static EM with(EntityManager em) {
return new EM(em);
}
public static <E> E ref(Class<E> clazz, Object id) {
return with(em()).ref(clazz, id);
}
public static <E> E get(Class<E> clazz, Object id) {
return with(em()).get(clazz, id);
}
public static <T> T find(Class<T> clazz, Object id) {
return with(em()).find(clazz, id);
}
public static <E> List<E> getAllEntities() {
return with(em()).getAll();
}
public static <E> List<E> getAll(Class<E> clazz, List<String> ids) {
return with(em()).getAll(clazz, ids);
}
public static <T> List<T> getAll(Class<T> clazz) {
return with(em()).getAll(clazz);
}
public static long count(Class<?> clazz) {
return with(em()).count(clazz);
}
public static Object save(Object record) {
return with(em()).save(record);
}
public static Object insert(Object entity) {
return with(em()).insert(entity);
}
public static void update(Object record) {
with(em()).update(record);
}
public static void delete(Object record) {
with(em()).delete(record);
}
public static <E> void delete(Class<E> clazz, Object id) {
with(em()).delete(clazz, id);
}
public static void refresh(Object entity) {
with(em()).refresh(entity);
}
public static void merge(Object entity) {
with(em()).merge(entity);
}
public static void transaction(Runnable action) {
tx(action, false);
}
public static void transactionReadOnly(Runnable action) {
tx(action, true);
}
private static void tx(Runnable action, boolean readOnly) {
Ctx ctx = Ctxs.get();
boolean newContext = ctx == null;
if (newContext) {
ctx = Ctxs.open("transaction");
}
try {
EntityManager em = ctx.persister();
with(em).transaction(action, readOnly);
} finally {
if (newContext) {
Ctxs.close();
}
}
}
public static void flush() {
em().flush();
}
public static void bootstrap(String[] path, Class<?>... providedEntities) {
JPAUtil.bootstrap(path, providedEntities);
}
public static boolean isLoaded(Object entity) {
return with(em()).isLoaded(entity);
}
public static boolean isLoaded(Object entity, String attribute) {
return with(em()).isLoaded(entity, attribute);
}
public static Object getIdentifier(Object entity) {
return with(em()).getIdentifier(entity);
}
public static <T> List<T> jpql(String jpql, Object... args) {
return with(em()).jpql(jpql, args);
}
public static <T> List<T> jpql(String jpql, Map<String, ?> namedArgs, Object... args) {
return with(em()).jpql(jpql, namedArgs, args);
}
public static boolean isEntity(Object obj) {
return JPAUtil.isEntity(obj);
}
public static List<EntityType<?>> getEntityTypes() {
return with(em()).getEntityTypes();
}
public static List<String> entities() {
return JPAUtil.entities;
}
public static List<Class<?>> getEntityJavaTypes() {
return JPAUtil.entityJavaTypes;
}
public static EntityManagerFactory emf() {
return JPAUtil.emf;
}
public static void emf(EntityManagerFactory emf) {
JPAUtil.emf(emf);
}
public static <T> T unproxy(T entity) {
return JPAUtil.unproxy(entity);
}
}