mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Polishing
This commit is contained in:
@@ -41,8 +41,8 @@ abstract class CoroutinesUtils {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
@Nullable
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
static Object awaitSingleOrNull(@Nullable Object value, Object continuation) {
|
||||
return MonoKt.awaitSingleOrNull(value instanceof Mono mono ? mono : Mono.justOrEmpty(value),
|
||||
(Continuation<Object>) continuation);
|
||||
|
||||
+6
-5
@@ -398,15 +398,16 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage
|
||||
* Determine the PersistenceUnitInfo to use for the EntityManagerFactory
|
||||
* created by this bean.
|
||||
* <p>The default implementation reads in all persistence unit infos from
|
||||
* {@code persistence.xml}, as defined in the JPA specification.
|
||||
* If no entity manager name was specified, it takes the first info in the
|
||||
* array as returned by the reader. Otherwise, it checks for a matching name.
|
||||
* {@code persistence.xml}, as defined in the JPA specification, selecting a unit
|
||||
* by name. If no persistence unit name was specified, it takes the default one
|
||||
* if configured, or otherwise the first persistence unit as found by the reader.
|
||||
* @param persistenceUnitManager the PersistenceUnitManager to obtain from
|
||||
* @return the chosen PersistenceUnitInfo
|
||||
*/
|
||||
protected PersistenceUnitInfo determinePersistenceUnitInfo(PersistenceUnitManager persistenceUnitManager) {
|
||||
if (getPersistenceUnitName() != null) {
|
||||
return persistenceUnitManager.obtainPersistenceUnitInfo(getPersistenceUnitName());
|
||||
String persistenceUnitName = getPersistenceUnitName();
|
||||
if (persistenceUnitName != null) {
|
||||
return persistenceUnitManager.obtainPersistenceUnitInfo(persistenceUnitName);
|
||||
}
|
||||
else {
|
||||
return persistenceUnitManager.obtainDefaultPersistenceUnitInfo();
|
||||
|
||||
+17
-17
@@ -193,7 +193,7 @@ public class DefaultPersistenceUnitManager
|
||||
|
||||
/**
|
||||
* Set the {@link PersistenceManagedTypes} to use to build the list of managed types
|
||||
* as an alternative to entity scanning.
|
||||
* for the default persistence unit, as an alternative to entity scanning.
|
||||
* @param managedTypes the managed types
|
||||
* @since 6.0
|
||||
*/
|
||||
@@ -540,33 +540,33 @@ public class DefaultPersistenceUnitManager
|
||||
* @see #setPackagesToScan
|
||||
*/
|
||||
private SpringPersistenceUnitInfo buildDefaultPersistenceUnitInfo() {
|
||||
SpringPersistenceUnitInfo scannedUnit = new SpringPersistenceUnitInfo();
|
||||
SpringPersistenceUnitInfo defaultUnit = new SpringPersistenceUnitInfo();
|
||||
if (this.defaultPersistenceUnitName != null) {
|
||||
scannedUnit.setPersistenceUnitName(this.defaultPersistenceUnitName);
|
||||
defaultUnit.setPersistenceUnitName(this.defaultPersistenceUnitName);
|
||||
}
|
||||
scannedUnit.setExcludeUnlistedClasses(true);
|
||||
defaultUnit.setExcludeUnlistedClasses(true);
|
||||
|
||||
if (this.managedTypes != null) {
|
||||
applyManagedTypes(scannedUnit, this.managedTypes);
|
||||
applyManagedTypes(defaultUnit, this.managedTypes);
|
||||
}
|
||||
else if (this.packagesToScan != null) {
|
||||
PersistenceManagedTypesScanner scanner = new PersistenceManagedTypesScanner(
|
||||
this.resourcePatternResolver, this.managedClassNameFilter);
|
||||
applyManagedTypes(scannedUnit, scanner.scan(this.packagesToScan));
|
||||
applyManagedTypes(defaultUnit, scanner.scan(this.packagesToScan));
|
||||
}
|
||||
|
||||
if (this.mappingResources != null) {
|
||||
for (String mappingFileName : this.mappingResources) {
|
||||
scannedUnit.addMappingFileName(mappingFileName);
|
||||
defaultUnit.addMappingFileName(mappingFileName);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Resource ormXml = getOrmXmlForDefaultPersistenceUnit();
|
||||
if (ormXml != null) {
|
||||
scannedUnit.addMappingFileName(DEFAULT_ORM_XML_RESOURCE);
|
||||
if (scannedUnit.getPersistenceUnitRootUrl() == null) {
|
||||
defaultUnit.addMappingFileName(DEFAULT_ORM_XML_RESOURCE);
|
||||
if (defaultUnit.getPersistenceUnitRootUrl() == null) {
|
||||
try {
|
||||
scannedUnit.setPersistenceUnitRootUrl(
|
||||
defaultUnit.setPersistenceUnitRootUrl(
|
||||
PersistenceUnitReader.determinePersistenceUnitRootUrl(ormXml));
|
||||
}
|
||||
catch (IOException ex) {
|
||||
@@ -576,7 +576,7 @@ public class DefaultPersistenceUnitManager
|
||||
}
|
||||
}
|
||||
|
||||
return scannedUnit;
|
||||
return defaultUnit;
|
||||
}
|
||||
|
||||
private void applyManagedTypes(SpringPersistenceUnitInfo scannedUnit, PersistenceManagedTypes managedTypes) {
|
||||
@@ -639,9 +639,9 @@ public class DefaultPersistenceUnitManager
|
||||
|
||||
|
||||
/**
|
||||
* Return the specified PersistenceUnitInfo from this manager's cache
|
||||
* of processed persistence units, keeping it in the cache (i.e. not
|
||||
* 'obtaining' it for use but rather just accessing it for post-processing).
|
||||
* Return the specified {@link MutablePersistenceUnitInfo} from this manager's cache
|
||||
* of processed persistence units, keeping it in the cache (i.e. not 'obtaining' it
|
||||
* for use but rather just accessing it for post-processing).
|
||||
* <p>This can be used in {@link #postProcessPersistenceUnitInfo} implementations,
|
||||
* detecting existing persistence units of the same name and potentially merging them.
|
||||
* @param persistenceUnitName the name of the desired persistence unit
|
||||
@@ -654,12 +654,12 @@ public class DefaultPersistenceUnitManager
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook method allowing subclasses to customize each PersistenceUnitInfo.
|
||||
* Hook method allowing subclasses to customize each {@link MutablePersistenceUnitInfo}.
|
||||
* <p>The default implementation delegates to all registered PersistenceUnitPostProcessors.
|
||||
* It is usually preferable to register further entity classes, jar files etc there
|
||||
* rather than in a subclass of this manager, to be able to reuse the post-processors.
|
||||
* @param pui the chosen PersistenceUnitInfo, as read from {@code persistence.xml}.
|
||||
* Passed in as MutablePersistenceUnitInfo.
|
||||
* @param pui the chosen persistence unit configuration, as read from
|
||||
* {@code persistence.xml}. Passed in as MutablePersistenceUnitInfo.
|
||||
* @see #setPersistenceUnitPostProcessors
|
||||
*/
|
||||
protected void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
|
||||
|
||||
+4
-3
@@ -33,9 +33,10 @@ import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Spring's base implementation of the JPA
|
||||
* Spring's mutable equivalent of the JPA
|
||||
* {@link jakarta.persistence.spi.PersistenceUnitInfo} interface,
|
||||
* used to bootstrap an {@code EntityManagerFactory} in a container.
|
||||
* This is the type exposed to {@link PersistenceUnitPostProcessor}.
|
||||
*
|
||||
* <p>This implementation is largely a JavaBean, offering mutators
|
||||
* for all standard {@code PersistenceUnitInfo} properties.
|
||||
@@ -57,10 +58,10 @@ public class MutablePersistenceUnitInfo implements SmartPersistenceUnitInfo {
|
||||
private PersistenceUnitTransactionType transactionType;
|
||||
|
||||
@Nullable
|
||||
private DataSource nonJtaDataSource;
|
||||
private DataSource jtaDataSource;
|
||||
|
||||
@Nullable
|
||||
private DataSource jtaDataSource;
|
||||
private DataSource nonJtaDataSource;
|
||||
|
||||
private final List<String> mappingFileNames = new ArrayList<>();
|
||||
|
||||
|
||||
+1
@@ -57,6 +57,7 @@ public interface PersistenceManagedTypes {
|
||||
@Nullable
|
||||
URL getPersistenceUnitRootUrl();
|
||||
|
||||
|
||||
/**
|
||||
* Create an instance using the specified managed class names.
|
||||
* @param managedClassNames the managed class names
|
||||
|
||||
+2
@@ -40,6 +40,7 @@ class SimplePersistenceManagedTypes implements PersistenceManagedTypes {
|
||||
|
||||
SimplePersistenceManagedTypes(List<String> managedClassNames, List<String> managedPackages,
|
||||
@Nullable URL persistenceUnitRootUrl) {
|
||||
|
||||
this.managedClassNames = managedClassNames;
|
||||
this.managedPackages = managedPackages;
|
||||
this.persistenceUnitRootUrl = persistenceUnitRootUrl;
|
||||
@@ -49,6 +50,7 @@ class SimplePersistenceManagedTypes implements PersistenceManagedTypes {
|
||||
this(managedClassNames, managedPackages, null);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<String> getManagedClassNames() {
|
||||
return this.managedClassNames;
|
||||
|
||||
+10
-10
@@ -39,27 +39,27 @@ public abstract class AbstractEntityManagerFactoryBeanTests {
|
||||
|
||||
protected static EntityManagerFactory mockEmf;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
void setup() {
|
||||
mockEmf = mock();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
void cleanup() {
|
||||
assertThat(TransactionSynchronizationManager.getResourceMap()).isEmpty();
|
||||
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
|
||||
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
|
||||
assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isFalse();
|
||||
}
|
||||
|
||||
protected void checkInvariants(AbstractEntityManagerFactoryBean demf) {
|
||||
assertThat(EntityManagerFactory.class.isAssignableFrom(demf.getObjectType())).isTrue();
|
||||
Object gotObject = demf.getObject();
|
||||
boolean condition = gotObject instanceof EntityManagerFactoryInfo;
|
||||
assertThat(condition).as("Object created by factory implements EntityManagerFactoryInfo").isTrue();
|
||||
EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) demf.getObject();
|
||||
assertThat(demf.getObject()).as("Successive invocations of getObject() return same object").isSameAs(emfi);
|
||||
assertThat(demf.getObject()).isSameAs(emfi);
|
||||
protected void checkInvariants(AbstractEntityManagerFactoryBean emfb) {
|
||||
assertThat(EntityManagerFactory.class.isAssignableFrom(emfb.getObjectType())).isTrue();
|
||||
EntityManagerFactory emf = emfb.getObject();
|
||||
assertThat(emf instanceof EntityManagerFactoryInfo).as("Object created by factory implements EntityManagerFactoryInfo").isTrue();
|
||||
EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) emf;
|
||||
assertThat(emfb.getObject()).as("Successive invocations of getObject() return same object").isSameAs(emfi);
|
||||
assertThat(emfb.getObject()).isSameAs(emfi);
|
||||
assertThat(mockEmf).isSameAs(emfi.getNativeEntityManagerFactory());
|
||||
}
|
||||
|
||||
|
||||
+20
-21
@@ -22,7 +22,6 @@ import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import jakarta.persistence.spi.PersistenceUnitInfo;
|
||||
import jakarta.persistence.spi.PersistenceUnitTransactionType;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -54,7 +53,7 @@ class PersistenceXmlParsingTests {
|
||||
PersistenceUnitReader reader = new PersistenceUnitReader(
|
||||
new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
|
||||
String resource = "/org/springframework/orm/jpa/META-INF/persistence.xml";
|
||||
PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
SpringPersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
|
||||
assertThat(info).isNotNull();
|
||||
assertThat(info).hasSize(1);
|
||||
@@ -71,7 +70,7 @@ class PersistenceXmlParsingTests {
|
||||
PersistenceUnitReader reader = new PersistenceUnitReader(
|
||||
new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
|
||||
String resource = "/org/springframework/orm/jpa/persistence-example1.xml";
|
||||
PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
SpringPersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
|
||||
assertThat(info).isNotNull();
|
||||
assertThat(info).hasSize(1);
|
||||
@@ -85,7 +84,7 @@ class PersistenceXmlParsingTests {
|
||||
PersistenceUnitReader reader = new PersistenceUnitReader(
|
||||
new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
|
||||
String resource = "/org/springframework/orm/jpa/persistence-example2.xml";
|
||||
PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
SpringPersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
|
||||
assertThat(info).isNotNull();
|
||||
assertThat(info).hasSize(1);
|
||||
@@ -103,7 +102,7 @@ class PersistenceXmlParsingTests {
|
||||
PersistenceUnitReader reader = new PersistenceUnitReader(
|
||||
new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
|
||||
String resource = "/org/springframework/orm/jpa/persistence-example3.xml";
|
||||
PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
SpringPersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
|
||||
assertThat(info).isNotNull();
|
||||
assertThat(info).hasSize(1);
|
||||
@@ -129,7 +128,7 @@ class PersistenceXmlParsingTests {
|
||||
PersistenceUnitReader reader = new PersistenceUnitReader(
|
||||
new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
|
||||
String resource = "/org/springframework/orm/jpa/persistence-example4.xml";
|
||||
PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
SpringPersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
|
||||
assertThat(info).isNotNull();
|
||||
assertThat(info).hasSize(1);
|
||||
@@ -153,7 +152,7 @@ class PersistenceXmlParsingTests {
|
||||
PersistenceUnitReader reader = new PersistenceUnitReader(
|
||||
new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
|
||||
String resource = "/org/springframework/orm/jpa/persistence-example5.xml";
|
||||
PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
SpringPersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
|
||||
assertThat(info).isNotNull();
|
||||
assertThat(info).hasSize(1);
|
||||
@@ -183,11 +182,11 @@ class PersistenceXmlParsingTests {
|
||||
dataSourceLookup.setDataSources(dataSources);
|
||||
PersistenceUnitReader reader = new PersistenceUnitReader(
|
||||
new PathMatchingResourcePatternResolver(), dataSourceLookup);
|
||||
PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
SpringPersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
|
||||
assertThat(info).hasSize(2);
|
||||
|
||||
PersistenceUnitInfo pu1 = info[0];
|
||||
SpringPersistenceUnitInfo pu1 = info[0];
|
||||
|
||||
assertThat(pu1.getPersistenceUnitName()).isEqualTo("pu1");
|
||||
|
||||
@@ -210,7 +209,7 @@ class PersistenceXmlParsingTests {
|
||||
|
||||
assertThat(pu1.excludeUnlistedClasses()).as("Exclude unlisted should default false in 1.0.").isFalse();
|
||||
|
||||
PersistenceUnitInfo pu2 = info[1];
|
||||
SpringPersistenceUnitInfo pu2 = info[1];
|
||||
|
||||
assertThat(pu2.getTransactionType()).isSameAs(PersistenceUnitTransactionType.JTA);
|
||||
assertThat(pu2.getPersistenceProviderClassName()).isEqualTo("com.acme.AcmePersistence");
|
||||
@@ -234,7 +233,7 @@ class PersistenceXmlParsingTests {
|
||||
PersistenceUnitReader reader = new PersistenceUnitReader(
|
||||
new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
|
||||
String resource = "/org/springframework/orm/jpa/persistence-example6.xml";
|
||||
PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
SpringPersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
assertThat(info).hasSize(1);
|
||||
assertThat(info[0].getPersistenceUnitName()).isEqualTo("pu");
|
||||
assertThat(info[0].getProperties()).isEmpty();
|
||||
@@ -286,27 +285,27 @@ class PersistenceXmlParsingTests {
|
||||
PersistenceUnitReader reader = new PersistenceUnitReader(
|
||||
new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
|
||||
String resource = "/org/springframework/orm/jpa/persistence-exclude-1.0.xml";
|
||||
PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
SpringPersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
|
||||
assertThat(info).isNotNull();
|
||||
assertThat(info.length).as("The number of persistence units is incorrect.").isEqualTo(4);
|
||||
|
||||
PersistenceUnitInfo noExclude = info[0];
|
||||
SpringPersistenceUnitInfo noExclude = info[0];
|
||||
assertThat(noExclude).as("noExclude should not be null.").isNotNull();
|
||||
assertThat(noExclude.getPersistenceUnitName()).as("noExclude name is not correct.").isEqualTo("NoExcludeElement");
|
||||
assertThat(noExclude.excludeUnlistedClasses()).as("Exclude unlisted should default false in 1.0.").isFalse();
|
||||
|
||||
PersistenceUnitInfo emptyExclude = info[1];
|
||||
SpringPersistenceUnitInfo emptyExclude = info[1];
|
||||
assertThat(emptyExclude).as("emptyExclude should not be null.").isNotNull();
|
||||
assertThat(emptyExclude.getPersistenceUnitName()).as("emptyExclude name is not correct.").isEqualTo("EmptyExcludeElement");
|
||||
assertThat(emptyExclude.excludeUnlistedClasses()).as("emptyExclude should be true.").isTrue();
|
||||
|
||||
PersistenceUnitInfo trueExclude = info[2];
|
||||
SpringPersistenceUnitInfo trueExclude = info[2];
|
||||
assertThat(trueExclude).as("trueExclude should not be null.").isNotNull();
|
||||
assertThat(trueExclude.getPersistenceUnitName()).as("trueExclude name is not correct.").isEqualTo("TrueExcludeElement");
|
||||
assertThat(trueExclude.excludeUnlistedClasses()).as("trueExclude should be true.").isTrue();
|
||||
|
||||
PersistenceUnitInfo falseExclude = info[3];
|
||||
SpringPersistenceUnitInfo falseExclude = info[3];
|
||||
assertThat(falseExclude).as("falseExclude should not be null.").isNotNull();
|
||||
assertThat(falseExclude.getPersistenceUnitName()).as("falseExclude name is not correct.").isEqualTo("FalseExcludeElement");
|
||||
assertThat(falseExclude.excludeUnlistedClasses()).as("falseExclude should be false.").isFalse();
|
||||
@@ -317,27 +316,27 @@ class PersistenceXmlParsingTests {
|
||||
PersistenceUnitReader reader = new PersistenceUnitReader(
|
||||
new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
|
||||
String resource = "/org/springframework/orm/jpa/persistence-exclude-2.0.xml";
|
||||
PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
SpringPersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
|
||||
|
||||
assertThat(info).isNotNull();
|
||||
assertThat(info.length).as("The number of persistence units is incorrect.").isEqualTo(4);
|
||||
|
||||
PersistenceUnitInfo noExclude = info[0];
|
||||
SpringPersistenceUnitInfo noExclude = info[0];
|
||||
assertThat(noExclude).as("noExclude should not be null.").isNotNull();
|
||||
assertThat(noExclude.getPersistenceUnitName()).as("noExclude name is not correct.").isEqualTo("NoExcludeElement");
|
||||
assertThat(noExclude.excludeUnlistedClasses()).as("Exclude unlisted still defaults to false in 2.0.").isFalse();
|
||||
|
||||
PersistenceUnitInfo emptyExclude = info[1];
|
||||
SpringPersistenceUnitInfo emptyExclude = info[1];
|
||||
assertThat(emptyExclude).as("emptyExclude should not be null.").isNotNull();
|
||||
assertThat(emptyExclude.getPersistenceUnitName()).as("emptyExclude name is not correct.").isEqualTo("EmptyExcludeElement");
|
||||
assertThat(emptyExclude.excludeUnlistedClasses()).as("emptyExclude should be true.").isTrue();
|
||||
|
||||
PersistenceUnitInfo trueExclude = info[2];
|
||||
SpringPersistenceUnitInfo trueExclude = info[2];
|
||||
assertThat(trueExclude).as("trueExclude should not be null.").isNotNull();
|
||||
assertThat(trueExclude.getPersistenceUnitName()).as("trueExclude name is not correct.").isEqualTo("TrueExcludeElement");
|
||||
assertThat(trueExclude.excludeUnlistedClasses()).as("trueExclude should be true.").isTrue();
|
||||
|
||||
PersistenceUnitInfo falseExclude = info[3];
|
||||
SpringPersistenceUnitInfo falseExclude = info[3];
|
||||
assertThat(falseExclude).as("falseExclude should not be null.").isNotNull();
|
||||
assertThat(falseExclude.getPersistenceUnitName()).as("falseExclude name is not correct.").isEqualTo("FalseExcludeElement");
|
||||
assertThat(falseExclude.excludeUnlistedClasses()).as("falseExclude should be false.").isFalse();
|
||||
|
||||
Reference in New Issue
Block a user