Add DataSource configuration/exposure to LocalEntityManagerFactoryBean
Closes gh-32344
This commit is contained in:
parent
5acee7b22e
commit
fbc265b72b
|
@ -276,8 +276,9 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage
|
||||||
* @see jakarta.persistence.spi.PersistenceUnitInfo#getNonJtaDataSource()
|
* @see jakarta.persistence.spi.PersistenceUnitInfo#getNonJtaDataSource()
|
||||||
* @see #setPersistenceUnitManager
|
* @see #setPersistenceUnitManager
|
||||||
*/
|
*/
|
||||||
public void setDataSource(DataSource dataSource) {
|
public void setDataSource(@Nullable DataSource dataSource) {
|
||||||
this.internalPersistenceUnitManager.setDataSourceLookup(new SingleDataSourceLookup(dataSource));
|
this.internalPersistenceUnitManager.setDataSourceLookup(
|
||||||
|
dataSource != null ? new SingleDataSourceLookup(dataSource) : null);
|
||||||
this.internalPersistenceUnitManager.setDefaultDataSource(dataSource);
|
this.internalPersistenceUnitManager.setDefaultDataSource(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -293,8 +294,9 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage
|
||||||
* @see jakarta.persistence.spi.PersistenceUnitInfo#getJtaDataSource()
|
* @see jakarta.persistence.spi.PersistenceUnitInfo#getJtaDataSource()
|
||||||
* @see #setPersistenceUnitManager
|
* @see #setPersistenceUnitManager
|
||||||
*/
|
*/
|
||||||
public void setJtaDataSource(DataSource jtaDataSource) {
|
public void setJtaDataSource(@Nullable DataSource jtaDataSource) {
|
||||||
this.internalPersistenceUnitManager.setDataSourceLookup(new SingleDataSourceLookup(jtaDataSource));
|
this.internalPersistenceUnitManager.setDataSourceLookup(
|
||||||
|
jtaDataSource != null ? new SingleDataSourceLookup(jtaDataSource) : null);
|
||||||
this.internalPersistenceUnitManager.setDefaultJtaDataSource(jtaDataSource);
|
this.internalPersistenceUnitManager.setDefaultJtaDataSource(jtaDataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -439,6 +441,7 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Nullable
|
||||||
public DataSource getDataSource() {
|
public DataSource getDataSource() {
|
||||||
if (this.persistenceUnitInfo != null) {
|
if (this.persistenceUnitInfo != null) {
|
||||||
return (this.persistenceUnitInfo.getJtaDataSource() != null ?
|
return (this.persistenceUnitInfo.getJtaDataSource() != null ?
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2021 the original author or authors.
|
* Copyright 2002-2024 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,11 +16,15 @@
|
||||||
|
|
||||||
package org.springframework.orm.jpa;
|
package org.springframework.orm.jpa;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import jakarta.persistence.EntityManagerFactory;
|
import jakarta.persistence.EntityManagerFactory;
|
||||||
import jakarta.persistence.Persistence;
|
import jakarta.persistence.Persistence;
|
||||||
import jakarta.persistence.PersistenceException;
|
import jakarta.persistence.PersistenceException;
|
||||||
import jakarta.persistence.spi.PersistenceProvider;
|
import jakarta.persistence.spi.PersistenceProvider;
|
||||||
|
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link org.springframework.beans.factory.FactoryBean} that creates a JPA
|
* {@link org.springframework.beans.factory.FactoryBean} that creates a JPA
|
||||||
* {@link jakarta.persistence.EntityManagerFactory} according to JPA's standard
|
* {@link jakarta.persistence.EntityManagerFactory} according to JPA's standard
|
||||||
|
@ -28,28 +32,18 @@ import jakarta.persistence.spi.PersistenceProvider;
|
||||||
* shared JPA EntityManagerFactory in a Spring application context; the
|
* shared JPA EntityManagerFactory in a Spring application context; the
|
||||||
* EntityManagerFactory can then be passed to JPA-based DAOs via
|
* EntityManagerFactory can then be passed to JPA-based DAOs via
|
||||||
* dependency injection. Note that switching to a JNDI lookup or to a
|
* dependency injection. Note that switching to a JNDI lookup or to a
|
||||||
* {@link LocalContainerEntityManagerFactoryBean}
|
* {@link LocalContainerEntityManagerFactoryBean} definition based on the
|
||||||
* definition is just a matter of configuration!
|
* JPA container contract is just a matter of configuration!
|
||||||
*
|
*
|
||||||
* <p>Configuration settings are usually read from a {@code META-INF/persistence.xml}
|
* <p>Configuration settings are usually read from a {@code META-INF/persistence.xml}
|
||||||
* config file, residing in the class path, according to the JPA standalone bootstrap
|
* config file, residing in the class path, according to the JPA standalone bootstrap
|
||||||
* contract. Additionally, most JPA providers will require a special VM agent
|
* contract. See the Java Persistence API specification and your persistence provider
|
||||||
* (specified on JVM startup) that allows them to instrument application classes.
|
* documentation for setup details. Additionally, JPA properties can also be added
|
||||||
* See the Java Persistence API specification and your provider documentation
|
* on this FactoryBean via {@link #setJpaProperties}/{@link #setJpaPropertyMap}.
|
||||||
* for setup details.
|
|
||||||
*
|
|
||||||
* <p>This EntityManagerFactory bootstrap is appropriate for standalone applications
|
|
||||||
* which solely use JPA for data access. If you want to set up your persistence
|
|
||||||
* provider for an external DataSource and/or for global transactions which span
|
|
||||||
* multiple resources, you will need to either deploy it into a full Jakarta EE
|
|
||||||
* application server and access the deployed EntityManagerFactory via JNDI,
|
|
||||||
* or use Spring's {@link LocalContainerEntityManagerFactoryBean} with appropriate
|
|
||||||
* configuration for local setup according to JPA's container contract.
|
|
||||||
*
|
*
|
||||||
* <p><b>Note:</b> This FactoryBean has limited configuration power in terms of
|
* <p><b>Note:</b> This FactoryBean has limited configuration power in terms of
|
||||||
* what configuration it is able to pass to the JPA provider. If you need more
|
* the configuration that it is able to pass to the JPA provider. If you need
|
||||||
* flexible configuration, for example passing a Spring-managed JDBC DataSource
|
* more flexible configuration options, consider using Spring's more powerful
|
||||||
* to the JPA provider, consider using Spring's more powerful
|
|
||||||
* {@link LocalContainerEntityManagerFactoryBean} instead.
|
* {@link LocalContainerEntityManagerFactoryBean} instead.
|
||||||
*
|
*
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
@ -67,6 +61,42 @@ import jakarta.persistence.spi.PersistenceProvider;
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public class LocalEntityManagerFactoryBean extends AbstractEntityManagerFactoryBean {
|
public class LocalEntityManagerFactoryBean extends AbstractEntityManagerFactoryBean {
|
||||||
|
|
||||||
|
private static final String DATASOURCE_PROPERTY = "jakarta.persistence.dataSource";
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specify the JDBC DataSource that the JPA persistence provider is supposed
|
||||||
|
* to use for accessing the database. This is an alternative to keeping the
|
||||||
|
* JDBC configuration in {@code persistence.xml}, passing in a Spring-managed
|
||||||
|
* DataSource through the "jakarta.persistence.dataSource" property instead.
|
||||||
|
* <p>When configured here, the JDBC DataSource will also get autodetected by
|
||||||
|
* {@link JpaTransactionManager} for exposing JPA transactions to JDBC accessors.
|
||||||
|
* @since 6.2
|
||||||
|
* @see #getJpaPropertyMap()
|
||||||
|
* @see JpaTransactionManager#setDataSource
|
||||||
|
*/
|
||||||
|
public void setDataSource(@Nullable DataSource dataSource) {
|
||||||
|
if (dataSource != null) {
|
||||||
|
getJpaPropertyMap().put(DATASOURCE_PROPERTY, dataSource);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
getJpaPropertyMap().remove(DATASOURCE_PROPERTY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expose the JDBC DataSource from the "jakarta.persistence.dataSource"
|
||||||
|
* property, if any.
|
||||||
|
* @since 6.2
|
||||||
|
* @see #getJpaPropertyMap()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public DataSource getDataSource() {
|
||||||
|
return (DataSource) getJpaPropertyMap().get(DATASOURCE_PROPERTY);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the EntityManagerFactory for the given configuration.
|
* Initialize the EntityManagerFactory for the given configuration.
|
||||||
* @throws jakarta.persistence.PersistenceException in case of JPA initialization errors
|
* @throws jakarta.persistence.PersistenceException in case of JPA initialization errors
|
||||||
|
|
Loading…
Reference in New Issue