Skip to content

Commit 107c8cc

Browse files
committed
add and spec PersistenceConfiguration
see jakartaee#358, jakartaee#149, jakartaee#114
1 parent 5604d11 commit 107c8cc

File tree

6 files changed

+466
-12
lines changed

6 files changed

+466
-12
lines changed

api/src/main/java/jakarta/persistence/Persistence.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,36 @@ public static EntityManagerFactory createEntityManagerFactory(String persistence
8888
return emf;
8989
}
9090

91+
/**
92+
* Create and return an EntityManagerFactory for the named persistence unit
93+
* using the given properties.
94+
*
95+
* @param configuration
96+
* configuration of the persistence unit
97+
* @return the factory that creates EntityManagers configured according to
98+
* the specified persistence unit.
99+
*
100+
* @since 3.2
101+
*/
102+
public static EntityManagerFactory createEntityManagerFactory(PersistenceConfiguration configuration) {
103+
104+
EntityManagerFactory emf = null;
105+
PersistenceProviderResolver resolver = PersistenceProviderResolverHolder.getPersistenceProviderResolver();
106+
107+
List<PersistenceProvider> providers = resolver.getPersistenceProviders();
108+
109+
for (PersistenceProvider provider : providers) {
110+
emf = provider.createEntityManagerFactory(configuration);
111+
if (emf != null) {
112+
break;
113+
}
114+
}
115+
if (emf == null) {
116+
throw new PersistenceException("No Persistence provider for EntityManager named " + configuration.name());
117+
}
118+
return emf;
119+
}
120+
91121

92122
/**
93123
* Create database schemas and/or tables and/or create DDL
Lines changed: 319 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,319 @@
1+
/*
2+
* Copyright (c) 2008, 2020 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0,
7+
* or the Eclipse Distribution License v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
// Contributors:
14+
// Gavin King - 3.2
15+
package jakarta.persistence;
16+
17+
import javax.sql.DataSource;
18+
import java.util.ArrayList;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
/**
24+
* Represents a configuration of a persistence unit, allowing programmatic
25+
* creation of an {@link EntityManagerFactory}. The configuration options
26+
* available via this API reflect the similarly-named elements of the
27+
* {@code persistence.xml} file.
28+
*
29+
* <p>This API may not be used to configure a container-managed persistence
30+
* unit. That is, the configured persistence unit should be considered a
31+
* Java SE persistence unit, even when this API is used within the Jakarta
32+
* EE environment.
33+
*
34+
* <p>If injection of the {@link EntityManagerFactory} is required, a CDI
35+
* {@code Producer} may be used to make the {@link EntityManagerFactory}
36+
* available as a CDI managed bean.
37+
*
38+
* <pre>
39+
* &#064;Produces &#064;ApplicationScoped &#064;Documents
40+
* EntityManagerFactory configure() {
41+
* return new PersistenceConfiguration()
42+
* .name("DocumentData")
43+
* .nonJtaDataSource(java:global/jdbc/DocumentDatabase")
44+
* .managedClass(Document.class)
45+
* .createEntityManagerFactory();
46+
* }
47+
* </pre>
48+
*
49+
* <p>Similarly, if injection of an {@link EntityManager} is required,
50+
* a CDI {@code Producer} method/{@code Disposer} method pair may be
51+
* used to make the {@link EntityManager} available as a CDI managed
52+
* bean.
53+
*
54+
* <pre>
55+
* &#064;Produces &#064;TransactionScoped &#064;Documents
56+
* EntityManager create(&#064;Documents EntityManagerFactory factory) {
57+
* return factory.createEntityManager();
58+
* }
59+
*
60+
* void close(&#064;Disposes &#064;Documents EntityManager entityManager) {
61+
* entityManager.close();
62+
* }
63+
* </pre>
64+
*
65+
* @see Persistence#createEntityManagerFactory(PersistenceConfiguration)
66+
*
67+
* @since 3.2
68+
*/
69+
public class PersistenceConfiguration {
70+
71+
private String name;
72+
private String provider;
73+
private String jtaDataSource;
74+
private String nonJtaDataSource;
75+
// private URL persistenceUnitRootUrl;
76+
77+
private SharedCacheMode sharedCacheMode = SharedCacheMode.UNSPECIFIED;
78+
private ValidationMode validationMode = ValidationMode.AUTO;
79+
private TransactionType transactionType = TransactionType.RESOURCE_LOCAL;
80+
// private boolean excludeUnlistedClasses = false;
81+
82+
// private List<URL> jarFileUrls = new ArrayList<>();
83+
private List<Class<?>> managedClasses = new ArrayList<>();
84+
private List<String> mappingFileNames = new ArrayList<>();
85+
private Map<String,Object> properties = new HashMap<>();
86+
87+
public enum TransactionType { JTA, RESOURCE_LOCAL }
88+
89+
/**
90+
* Create a new {@link EntityManagerFactory} based on this configuration.
91+
* @throws IllegalStateException if required configuration is missing
92+
*/
93+
public EntityManagerFactory createEntityManagerFactory() {
94+
return Persistence.createEntityManagerFactory(this);
95+
}
96+
97+
/**
98+
* Specify the name of the persistence unit.
99+
*/
100+
public PersistenceConfiguration name(String name) {
101+
this.name = name;
102+
return this;
103+
}
104+
105+
/**
106+
* @return the name of the persistence unit.
107+
*/
108+
public String name() {
109+
return name;
110+
}
111+
112+
// /**
113+
// * Specify a root URL for the persistence unit.
114+
// * @param url the URL of a directory or JAR archive
115+
// * @return this configuration
116+
// */
117+
// public PersistenceConfiguration persistenceUnitRootUrl(URL url) {
118+
// persistenceUnitRootUrl = url;
119+
// return this;
120+
// }
121+
//
122+
// /**
123+
// * @return the root URL for the persistence unit, if any
124+
// */
125+
// public URL persistenceUnitRootUrl() {
126+
// return persistenceUnitRootUrl;
127+
// }
128+
129+
/**
130+
* Specify the persistence provider.
131+
* @param providerClassName the qualified name of the persistence provider class
132+
* @return this configuration
133+
*/
134+
public PersistenceConfiguration provider(String providerClassName) {
135+
this.provider = providerClassName;
136+
return this;
137+
}
138+
139+
/**
140+
* @return the qualified name of the persistence provider class.
141+
*/
142+
public String provider() {
143+
return provider;
144+
}
145+
146+
/**
147+
* Specify a JTA {@link DataSource}.
148+
* @param dataSourceJndiName the JNDI name of a JTA datasource
149+
* @return this configuration
150+
*/
151+
public PersistenceConfiguration jtaDataSource(String dataSourceJndiName) {
152+
this.jtaDataSource = dataSourceJndiName;
153+
return this;
154+
}
155+
156+
/**
157+
* @return the configured JTA datasource, if any
158+
*/
159+
public String jtaDataSource() {
160+
return jtaDataSource;
161+
}
162+
163+
/**
164+
* Specify a non-JTA {@link DataSource}.
165+
* @param dataSourceJndiName the JNDI name of a non-JTA datasource
166+
* @return this configuration
167+
*/
168+
public PersistenceConfiguration nonJtaDataSource(String dataSourceJndiName) {
169+
this.nonJtaDataSource = dataSourceJndiName;
170+
return this;
171+
}
172+
173+
/**
174+
* @return the configured non-JTA datasource, if any
175+
*/
176+
public String nonJtaDataSource() {
177+
return nonJtaDataSource;
178+
}
179+
180+
/**
181+
* Add a managed class (an {@link Entity}, {@link Embeddable}, or
182+
* {@link MappedSuperclass}) to the configuration.
183+
* @param managedClass the managed class
184+
* @return this configuration
185+
*/
186+
public PersistenceConfiguration managedClass(Class<?> managedClass) {
187+
managedClasses.add(managedClass);
188+
return this;
189+
}
190+
191+
/**
192+
* @return all configured managed classes
193+
*/
194+
public List<Class<?>> managedClasses() {
195+
return managedClasses;
196+
}
197+
198+
/**
199+
* Add an {@code orm.xml} mapping file name to the configuration.
200+
* @param name the file path of the mapping file
201+
* @return this configuration
202+
*/
203+
public PersistenceConfiguration mappingFile(String name) {
204+
mappingFileNames.add(name);
205+
return this;
206+
}
207+
208+
/**
209+
* @return all configured mapping file names
210+
*/
211+
public List<String> mappingFiles() {
212+
return mappingFileNames;
213+
}
214+
215+
// /**
216+
// * Add a JAR archive to the configuration.
217+
// * @param url the URL of the JAR archive
218+
// * @return this configuration
219+
// */
220+
// public PersistenceConfiguration jarFileUrl(URL url) {
221+
// jarFileUrls.add(url);
222+
// return this;
223+
// }
224+
//
225+
// /**
226+
// * @return all configured JAR archives
227+
// */
228+
// public List<URL> jarFileUrls() {
229+
// return jarFileUrls;
230+
// }
231+
232+
/**
233+
* Specify the transaction type for the persistence unit.
234+
* @param transactionType the transaction type
235+
* @return this configuration
236+
*/
237+
public PersistenceConfiguration transactionType(TransactionType transactionType) {
238+
this.transactionType = transactionType;
239+
return this;
240+
}
241+
242+
/**
243+
* @return the transaction type
244+
*/
245+
public TransactionType transactionType() {
246+
return transactionType;
247+
}
248+
249+
/**
250+
* Specify the shared cache mode for the persistence unit.
251+
* @param sharedCacheMode the shared cache mode
252+
* @return this configuration
253+
*/
254+
public PersistenceConfiguration sharedCacheMode(SharedCacheMode sharedCacheMode) {
255+
this.sharedCacheMode = sharedCacheMode;
256+
return this;
257+
}
258+
259+
/**
260+
* @return the shared cache mode
261+
*/
262+
public SharedCacheMode sharedCacheMode() {
263+
return sharedCacheMode;
264+
}
265+
266+
/**
267+
* Specify the validation mode for the persistence unit.
268+
* @param validationMode the shared cache mode
269+
* @return this configuration
270+
*/
271+
public PersistenceConfiguration validationMode(ValidationMode validationMode) {
272+
this.validationMode = validationMode;
273+
return this;
274+
}
275+
276+
/**
277+
* @return the validation mode
278+
*/
279+
public ValidationMode validationMode() {
280+
return validationMode;
281+
}
282+
283+
// public PersistenceConfiguration excludeUnlistedClasses(boolean excludeUnlistedClasses) {
284+
// this.excludeUnlistedClasses = excludeUnlistedClasses;
285+
// return this;
286+
// }
287+
//
288+
// public boolean excludeUnlistedClasses() {
289+
// return excludeUnlistedClasses;
290+
// }
291+
292+
/**
293+
* Set a property of this persistence unit.
294+
* @param name the property name
295+
* @param value the property value
296+
* @return this configuration
297+
*/
298+
public PersistenceConfiguration property(String name, Object value) {
299+
properties.put(name, value);
300+
return this;
301+
}
302+
303+
/**
304+
* Set multiple properties of this persistence unit.
305+
* @param properties the properties
306+
* @return this configuration
307+
*/
308+
public PersistenceConfiguration properties(Map<String,?> properties) {
309+
this.properties.putAll(properties);
310+
return this;
311+
}
312+
313+
/**
314+
* @return the configured properties
315+
*/
316+
public Map<String, Object> properties() {
317+
return properties;
318+
}
319+
}

api/src/main/java/jakarta/persistence/spi/PersistenceProvider.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import jakarta.persistence.EntityManagerFactory;
2020
import jakarta.persistence.Persistence;
21+
import jakarta.persistence.PersistenceConfiguration;
2122
import jakarta.persistence.PersistenceException;
2223
import java.util.Map;
2324

@@ -45,10 +46,27 @@ public interface PersistenceProvider {
4546
* properties not specified in the <code>persistence.xml</code>
4647
* (and may be null if no properties are specified).
4748
* @return EntityManagerFactory for the persistence unit,
48-
* or null if the provider is not the right provider
49+
* or null if the provider is not the right provider
50+
*
51+
* @see Persistence#createEntityManagerFactory(String, Map)
4952
*/
5053
public EntityManagerFactory createEntityManagerFactory(String emName, Map map);
5154

55+
/**
56+
* Called by <code>Persistence</code> class when an
57+
* <code>EntityManagerFactory</code> is to be created.
58+
*
59+
* @param configuration the configuration of the persistence unit
60+
* @return EntityManagerFactory for the persistence unit,
61+
* or null if the provider is not the right provider
62+
* @throws IllegalStateException if required configuration is missing
63+
*
64+
* @see Persistence#createEntityManagerFactory(PersistenceConfiguration)
65+
*
66+
* @since 3.2
67+
*/
68+
public EntityManagerFactory createEntityManagerFactory(PersistenceConfiguration configuration);
69+
5270
/**
5371
* Called by the container when an <code>EntityManagerFactory</code>
5472
* is to be created.

0 commit comments

Comments
 (0)