|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2021 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.orm.jpa;
|
18 | 18 |
|
| 19 | +import javax.sql.DataSource; |
| 20 | + |
19 | 21 | import jakarta.persistence.EntityManagerFactory;
|
20 | 22 | import jakarta.persistence.Persistence;
|
21 | 23 | import jakarta.persistence.PersistenceException;
|
22 | 24 | import jakarta.persistence.spi.PersistenceProvider;
|
23 | 25 |
|
| 26 | +import org.springframework.lang.Nullable; |
| 27 | + |
24 | 28 | /**
|
25 | 29 | * {@link org.springframework.beans.factory.FactoryBean} that creates a JPA
|
26 | 30 | * {@link jakarta.persistence.EntityManagerFactory} according to JPA's standard
|
27 | 31 | * <i>standalone</i> bootstrap contract. This is the simplest way to set up a
|
28 | 32 | * shared JPA EntityManagerFactory in a Spring application context; the
|
29 | 33 | * EntityManagerFactory can then be passed to JPA-based DAOs via
|
30 | 34 | * dependency injection. Note that switching to a JNDI lookup or to a
|
31 |
| - * {@link LocalContainerEntityManagerFactoryBean} |
32 |
| - * definition is just a matter of configuration! |
| 35 | + * {@link LocalContainerEntityManagerFactoryBean} definition based on the |
| 36 | + * JPA container contract is just a matter of configuration! |
33 | 37 | *
|
34 | 38 | * <p>Configuration settings are usually read from a {@code META-INF/persistence.xml}
|
35 | 39 | * config file, residing in the class path, according to the JPA standalone bootstrap
|
36 |
| - * contract. Additionally, most JPA providers will require a special VM agent |
37 |
| - * (specified on JVM startup) that allows them to instrument application classes. |
38 |
| - * See the Java Persistence API specification and your provider documentation |
39 |
| - * for setup details. |
40 |
| - * |
41 |
| - * <p>This EntityManagerFactory bootstrap is appropriate for standalone applications |
42 |
| - * which solely use JPA for data access. If you want to set up your persistence |
43 |
| - * provider for an external DataSource and/or for global transactions which span |
44 |
| - * multiple resources, you will need to either deploy it into a full Jakarta EE |
45 |
| - * application server and access the deployed EntityManagerFactory via JNDI, |
46 |
| - * or use Spring's {@link LocalContainerEntityManagerFactoryBean} with appropriate |
47 |
| - * configuration for local setup according to JPA's container contract. |
| 40 | + * contract. See the Java Persistence API specification and your persistence provider |
| 41 | + * documentation for setup details. Additionally, JPA properties can also be added |
| 42 | + * on this FactoryBean via {@link #setJpaProperties}/{@link #setJpaPropertyMap}. |
48 | 43 | *
|
49 | 44 | * <p><b>Note:</b> This FactoryBean has limited configuration power in terms of
|
50 |
| - * what configuration it is able to pass to the JPA provider. If you need more |
51 |
| - * flexible configuration, for example passing a Spring-managed JDBC DataSource |
52 |
| - * to the JPA provider, consider using Spring's more powerful |
| 45 | + * the configuration that it is able to pass to the JPA provider. If you need |
| 46 | + * more flexible configuration options, consider using Spring's more powerful |
53 | 47 | * {@link LocalContainerEntityManagerFactoryBean} instead.
|
54 | 48 | *
|
55 | 49 | * @author Juergen Hoeller
|
|
67 | 61 | @SuppressWarnings("serial")
|
68 | 62 | public class LocalEntityManagerFactoryBean extends AbstractEntityManagerFactoryBean {
|
69 | 63 |
|
| 64 | + private static final String DATASOURCE_PROPERTY = "jakarta.persistence.dataSource"; |
| 65 | + |
| 66 | + |
| 67 | + /** |
| 68 | + * Specify the JDBC DataSource that the JPA persistence provider is supposed |
| 69 | + * to use for accessing the database. This is an alternative to keeping the |
| 70 | + * JDBC configuration in {@code persistence.xml}, passing in a Spring-managed |
| 71 | + * DataSource through the "jakarta.persistence.dataSource" property instead. |
| 72 | + * <p>When configured here, the JDBC DataSource will also get autodetected by |
| 73 | + * {@link JpaTransactionManager} for exposing JPA transactions to JDBC accessors. |
| 74 | + * @since 6.2 |
| 75 | + * @see #getJpaPropertyMap() |
| 76 | + * @see JpaTransactionManager#setDataSource |
| 77 | + */ |
| 78 | + public void setDataSource(@Nullable DataSource dataSource) { |
| 79 | + if (dataSource != null) { |
| 80 | + getJpaPropertyMap().put(DATASOURCE_PROPERTY, dataSource); |
| 81 | + } |
| 82 | + else { |
| 83 | + getJpaPropertyMap().remove(DATASOURCE_PROPERTY); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Expose the JDBC DataSource from the "jakarta.persistence.dataSource" |
| 89 | + * property, if any. |
| 90 | + * @since 6.2 |
| 91 | + * @see #getJpaPropertyMap() |
| 92 | + */ |
| 93 | + @Override |
| 94 | + @Nullable |
| 95 | + public DataSource getDataSource() { |
| 96 | + return (DataSource) getJpaPropertyMap().get(DATASOURCE_PROPERTY); |
| 97 | + } |
| 98 | + |
| 99 | + |
70 | 100 | /**
|
71 | 101 | * Initialize the EntityManagerFactory for the given configuration.
|
72 | 102 | * @throws jakarta.persistence.PersistenceException in case of JPA initialization errors
|
|
0 commit comments