-
Notifications
You must be signed in to change notification settings - Fork 14
/
Persistence.java
132 lines (111 loc) · 5.22 KB
/
Persistence.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
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa;
import cz.cvut.kbss.jopa.model.*;
import java.util.*;
/**
* Bootstrap class that is used to obtain an {@link EntityManagerFactory}.
* <p>
* The {@code Persistence} class is also used to obtain a {@link PersistenceUtil} instance.
*/
public class Persistence {
private static final PersistenceUtil pu = new PersistenceUtilImpl();
private Persistence() {
throw new AssertionError("No Persistence instances can be created.");
}
/**
* Create and return an EntityManagerFactory for the named persistence unit.
*
* @param persistenceUnitName the name of the persistence unit
* @return the factory that creates EntityManagers configured according to the specified persistence unit
*/
public static EntityManagerFactory createEntityManagerFactory(final String persistenceUnitName) {
return createEntityManagerFactory(persistenceUnitName, Collections.emptyMap());
}
/**
* Create and return an EntityManagerFactory for the named persistence unit using the given properties.
*
* @param persistenceUnitName the name of the persistence unit
* @param properties Additional properties to use when creating the factory. The values of these properties
* override any values that may have been configured elsewhere
* @return the factory that creates EntityManagers configured according to the specified persistence unit
*/
public static EntityManagerFactory createEntityManagerFactory(final String persistenceUnitName,
final Map<String, String> properties) {
final Map<String, String> realParams = new HashMap<>(properties);
final String className = realParams.get(PersistenceProperties.JPA_PERSISTENCE_PROVIDER);
if (className == null) {
throw new IllegalArgumentException("Missing persistence unit provider.");
}
final List<PersistenceProvider> providers = PersistenceProviderResolverHolder
.getPersistenceProviderResolver().getPersistenceProviders();
final Optional<PersistenceProvider> provider = providers.stream().filter(pp -> pp.getClass().getName()
.equals(className))
.findFirst();
return provider.orElseThrow(() -> new IllegalArgumentException(
"Type " + className + " is not a PersistenceProvider implementation."))
.createEntityManagerFactory(persistenceUnitName, realParams);
}
/**
* Return the PersistenceUtil instance
*
* @return PersistenceUtil instance
*/
public static PersistenceUtil getPersistenceUtil() {
return pu;
}
}
class PersistenceUtilImpl implements PersistenceUtil {
@Override
public boolean isLoaded(Object entity, String attributeName) {
for (final PersistenceProvider pp : PersistenceProviderResolverHolder.getPersistenceProviderResolver()
.getPersistenceProviders()) {
switch (pp.getProviderUtil().isLoadedWithoutReference(entity, attributeName)) {
case LOADED:
return true;
case NOT_LOADED:
return false;
default:
break;
}
}
for (final PersistenceProvider pp : PersistenceProviderResolverHolder.getPersistenceProviderResolver()
.getPersistenceProviders()) {
switch (pp.getProviderUtil().isLoadedWithReference(entity, attributeName)) {
case LOADED:
return true;
case NOT_LOADED:
return false;
default:
break;
}
}
return false;
}
@Override
public boolean isLoaded(Object entity) {
for (final PersistenceProvider pp : PersistenceProviderResolverHolder.getPersistenceProviderResolver()
.getPersistenceProviders()) {
switch (pp.getProviderUtil().isLoaded(entity)) {
case LOADED:
return true;
case NOT_LOADED:
return false;
default:
}
}
return false;
}
}