Browse Source

Add setPackagesToScan to LocalEntityManagerFactoryBean (and make setDataSource work)

Includes fix for consistent PersistenceException in case of no unit found for name.
Includes proper tests for Local(Container)EntityManagerFactoryBean with scan setup.

Closes gh-36270
Closes gh-36271
pull/36281/head
Juergen Hoeller 1 month ago
parent
commit
394a97d7e0
  1. 3
      spring-orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java
  2. 78
      spring-orm/src/main/java/org/springframework/orm/jpa/LocalEntityManagerFactoryBean.java
  3. 1
      spring-orm/src/test/java/org/springframework/orm/jpa/domain/Car.java
  4. 1
      spring-orm/src/test/java/org/springframework/orm/jpa/domain/Employee.java
  5. 2
      spring-orm/src/test/java/org/springframework/orm/jpa/domain/EmployeeCategory.java
  6. 3
      spring-orm/src/test/java/org/springframework/orm/jpa/domain/EmployeeCategoryConverter.java
  7. 2
      spring-orm/src/test/java/org/springframework/orm/jpa/domain/EmployeeKind.java
  8. 3
      spring-orm/src/test/java/org/springframework/orm/jpa/domain/EmployeeKindConverter.java
  9. 1
      spring-orm/src/test/java/org/springframework/orm/jpa/domain/EmployeeLocation.java
  10. 1
      spring-orm/src/test/java/org/springframework/orm/jpa/domain/EmployeeLocationConverter.java
  11. 30
      spring-orm/src/test/java/org/springframework/orm/jpa/eclipselink/EclipseLinkEntityManagerFactoryScanIntegrationTests.java
  12. 45
      spring-orm/src/test/java/org/springframework/orm/jpa/eclipselink/EclipseLinkEntityManagerFactorySimpleIntegrationTests.java
  13. 30
      spring-orm/src/test/java/org/springframework/orm/jpa/hibernate/HibernateEntityManagerFactoryScanIntegrationTests.java
  14. 45
      spring-orm/src/test/java/org/springframework/orm/jpa/hibernate/HibernateEntityManagerFactorySimpleIntegrationTests.java
  15. 3
      spring-orm/src/test/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesBeanRegistrationAotProcessorTests.java
  16. 14
      spring-orm/src/test/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesScannerTests.java
  17. 29
      spring-orm/src/test/resources/org/springframework/orm/jpa/eclipselink/eclipselink-manager-scan.xml
  18. 29
      spring-orm/src/test/resources/org/springframework/orm/jpa/eclipselink/eclipselink-manager-simple.xml
  19. 10
      spring-orm/src/test/resources/org/springframework/orm/jpa/eclipselink/eclipselink-manager.xml
  20. 42
      spring-orm/src/test/resources/org/springframework/orm/jpa/hibernate/hibernate-manager-scan.xml
  21. 42
      spring-orm/src/test/resources/org/springframework/orm/jpa/hibernate/hibernate-manager-simple.xml

3
spring-orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java

@ -86,14 +86,11 @@ import org.springframework.util.StringUtils; @@ -86,14 +86,11 @@ import org.springframework.util.StringUtils;
* @author Juergen Hoeller
* @author Rod Johnson
* @since 2.0
* @see #setPersistenceXmlLocation
* @see #setJpaProperties
* @see #setJpaVendorAdapter
* @see #setLoadTimeWeaver
* @see #setDataSource
* @see EntityManagerFactoryInfo
* @see LocalEntityManagerFactoryBean
* @see org.springframework.orm.jpa.support.SharedEntityManagerBean
* @see jakarta.persistence.spi.PersistenceProvider#createContainerEntityManagerFactory
*/
@SuppressWarnings("serial")

78
spring-orm/src/main/java/org/springframework/orm/jpa/LocalEntityManagerFactoryBean.java

@ -16,6 +16,9 @@ @@ -16,6 +16,9 @@
package org.springframework.orm.jpa;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.sql.DataSource;
import jakarta.persistence.EntityManagerFactory;
@ -25,8 +28,16 @@ import jakarta.persistence.PersistenceException; @@ -25,8 +28,16 @@ import jakarta.persistence.PersistenceException;
import jakarta.persistence.spi.PersistenceProvider;
import org.jspecify.annotations.Nullable;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager;
import org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypes;
import org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypesScanner;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* {@link org.springframework.beans.factory.FactoryBean} that creates a JPA
@ -54,18 +65,26 @@ import org.springframework.util.Assert; @@ -54,18 +65,26 @@ import org.springframework.util.Assert;
* @since 2.0
* @see #setJpaProperties
* @see #setJpaVendorAdapter
* @see JpaTransactionManager#setEntityManagerFactory
* @see #setPersistenceConfiguration
* @see #setDataSource
* @see LocalContainerEntityManagerFactoryBean
* @see org.springframework.jndi.JndiObjectFactoryBean
* @see org.springframework.orm.jpa.support.SharedEntityManagerBean
* @see jakarta.persistence.Persistence#createEntityManagerFactory
* @see jakarta.persistence.spi.PersistenceProvider#createEntityManagerFactory
*/
@SuppressWarnings("serial")
public class LocalEntityManagerFactoryBean extends AbstractEntityManagerFactoryBean {
public class LocalEntityManagerFactoryBean extends AbstractEntityManagerFactoryBean implements ResourceLoaderAware {
private static final String NON_JTA_DATASOURCE_PROPERTY = "jakarta.persistence.nonJtaDataSource";
private static final String PACKAGE_INFO_SUFFIX = ".package-info";
private @Nullable PersistenceConfiguration configuration;
private String @Nullable [] packagesToScan;
private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
/**
* Create a {@code LocalEntityManagerFactoryBean}.
@ -151,6 +170,27 @@ public class LocalEntityManagerFactoryBean extends AbstractEntityManagerFactoryB @@ -151,6 +170,27 @@ public class LocalEntityManagerFactoryBean extends AbstractEntityManagerFactoryB
super.setPersistenceUnitName(persistenceUnitName);
}
/**
* Set whether to use Spring-based scanning for entity classes in the classpath
* instead of using JPA's standard scanning of jar files with {@code persistence.xml}
* markers in them. In case of Spring-based scanning, no {@code persistence.xml}
* is necessary; all you need to do is to specify base packages to search here.
* <p>Default is none. Specify packages to search for autodetection of your entity
* classes in the classpath. This is analogous to Spring's component-scan feature
* ({@link org.springframework.context.annotation.ClassPathBeanDefinitionScanner}).
* <p>The use of this setter switches this {@code LocalEntityManagerFactoryBean}
* to a {@link #getPersistenceConfiguration() PersistenceConfiguration}, with no
* {@code persistence.xml} reading or provider-driven scanning happening anymore.
* Further JPA settings can be applied on the local {@link PersistenceConfiguration}
* via {@link #getPersistenceConfiguration()}.
* @since 7.0.4
* @see LocalContainerEntityManagerFactoryBean#setPackagesToScan
* @see org.springframework.orm.jpa.hibernate.LocalSessionFactoryBean#setPackagesToScan
*/
public void setPackagesToScan(String... packagesToScan) {
this.packagesToScan = packagesToScan;
}
/**
* Specify the JDBC DataSource that the JPA persistence provider is supposed
* to use for accessing the database. This is an alternative to keeping the
@ -165,9 +205,11 @@ public class LocalEntityManagerFactoryBean extends AbstractEntityManagerFactoryB @@ -165,9 +205,11 @@ public class LocalEntityManagerFactoryBean extends AbstractEntityManagerFactoryB
public void setDataSource(@Nullable DataSource dataSource) {
if (dataSource != null) {
getJpaPropertyMap().put(PersistenceConfiguration.JDBC_DATASOURCE, dataSource);
getJpaPropertyMap().put(NON_JTA_DATASOURCE_PROPERTY, dataSource);
}
else {
getJpaPropertyMap().remove(PersistenceConfiguration.JDBC_DATASOURCE);
getJpaPropertyMap().remove(NON_JTA_DATASOURCE_PROPERTY);
}
}
@ -182,6 +224,11 @@ public class LocalEntityManagerFactoryBean extends AbstractEntityManagerFactoryB @@ -182,6 +224,11 @@ public class LocalEntityManagerFactoryBean extends AbstractEntityManagerFactoryB
return (DataSource) getJpaPropertyMap().get(PersistenceConfiguration.JDBC_DATASOURCE);
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
}
/**
* Initialize the EntityManagerFactory for the given configuration.
@ -193,6 +240,25 @@ public class LocalEntityManagerFactoryBean extends AbstractEntityManagerFactoryB @@ -193,6 +240,25 @@ public class LocalEntityManagerFactoryBean extends AbstractEntityManagerFactoryB
logger.debug("Building JPA EntityManagerFactory for persistence unit '" + getPersistenceUnitName() + "'");
}
if (this.packagesToScan != null) {
PersistenceManagedTypesScanner scanner = new PersistenceManagedTypesScanner(this.resourcePatternResolver);
PersistenceManagedTypes result = scanner.scan(this.packagesToScan);
// Expose managed class names from scan result (on JPA 4.0+, this includes
// everything meta-annotated with @Discoverable, even package-info classes)
Set<String> classNameSet = new LinkedHashSet<>(result.getManagedClassNames());
// Expose managed packages as package-info class names if not included already
// (accepted by PersistenceConfiguration on Hibernate as well as EclipseLink)
for (String managedPackage : result.getManagedPackages()) {
classNameSet.add(managedPackage + PACKAGE_INFO_SUFFIX);
}
// Expose pre-resolved Class references to PersistenceConfiguration.
PersistenceConfiguration config = getPersistenceConfiguration();
ClassLoader classLoader = this.resourcePatternResolver.getClassLoader();
for (String className : classNameSet) {
config.managedClass(ClassUtils.resolveClassName(className, classLoader));
}
}
if (this.configuration != null) {
this.configuration.properties(getJpaPropertyMap());
}
@ -204,8 +270,8 @@ public class LocalEntityManagerFactoryBean extends AbstractEntityManagerFactoryB @@ -204,8 +270,8 @@ public class LocalEntityManagerFactoryBean extends AbstractEntityManagerFactoryB
provider.createEntityManagerFactory(this.configuration) :
provider.createEntityManagerFactory(getPersistenceUnitName(), getJpaPropertyMap()));
if (emf == null) {
throw new IllegalStateException(
"PersistenceProvider [" + provider + "] did not return an EntityManagerFactory for name '" +
throw new PersistenceException(
"PersistenceProvider [" + provider + "] could not find persistence unit for name '" +
getPersistenceUnitName() + "'");
}
return emf;

1
spring-orm/src/test/java/org/springframework/orm/jpa/domain/Car.java

@ -47,4 +47,5 @@ public class Car { @@ -47,4 +47,5 @@ public class Car {
String getModel() {
return model;
}
}

1
spring-orm/src/test/java/org/springframework/orm/jpa/domain/Employee.java

@ -86,4 +86,5 @@ public class Employee { @@ -86,4 +86,5 @@ public class Employee {
@PreRemove
public void preRemove() {
}
}

2
spring-orm/src/test/java/org/springframework/orm/jpa/domain/EmployeeCategory.java

@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
package org.springframework.orm.jpa.domain;
public class EmployeeCategory {
private String name;
public String getName() {
@ -26,4 +27,5 @@ public class EmployeeCategory { @@ -26,4 +27,5 @@ public class EmployeeCategory {
public void setName(String name) {
this.name = name;
}
}

3
spring-orm/src/test/java/org/springframework/orm/jpa/domain/EmployeeCategoryConverter.java

@ -17,7 +17,9 @@ @@ -17,7 +17,9 @@
package org.springframework.orm.jpa.domain;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
@Converter
public class EmployeeCategoryConverter implements AttributeConverter<EmployeeCategory, String> {
@Override
@ -37,4 +39,5 @@ public class EmployeeCategoryConverter implements AttributeConverter<EmployeeCat @@ -37,4 +39,5 @@ public class EmployeeCategoryConverter implements AttributeConverter<EmployeeCat
}
return null;
}
}

2
spring-orm/src/test/java/org/springframework/orm/jpa/domain/EmployeeKind.java

@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
package org.springframework.orm.jpa.domain;
public class EmployeeKind {
private String name;
public String getName() {
@ -26,4 +27,5 @@ public class EmployeeKind { @@ -26,4 +27,5 @@ public class EmployeeKind {
public void setName(String name) {
this.name = name;
}
}

3
spring-orm/src/test/java/org/springframework/orm/jpa/domain/EmployeeKindConverter.java

@ -17,7 +17,9 @@ @@ -17,7 +17,9 @@
package org.springframework.orm.jpa.domain;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
@Converter(autoApply = true)
public class EmployeeKindConverter implements AttributeConverter<EmployeeKind, String> {
@Override
@ -37,4 +39,5 @@ public class EmployeeKindConverter implements AttributeConverter<EmployeeKind, S @@ -37,4 +39,5 @@ public class EmployeeKindConverter implements AttributeConverter<EmployeeKind, S
}
return null;
}
}

1
spring-orm/src/test/java/org/springframework/orm/jpa/domain/EmployeeLocation.java

@ -27,4 +27,5 @@ public class EmployeeLocation { @@ -27,4 +27,5 @@ public class EmployeeLocation {
public void setLocation(String location) {
this.location = location;
}
}

1
spring-orm/src/test/java/org/springframework/orm/jpa/domain/EmployeeLocationConverter.java

@ -39,4 +39,5 @@ public class EmployeeLocationConverter implements AttributeConverter<EmployeeLoc @@ -39,4 +39,5 @@ public class EmployeeLocationConverter implements AttributeConverter<EmployeeLoc
}
return null;
}
}

30
spring-orm/src/test/java/org/springframework/orm/jpa/eclipselink/EclipseLinkEntityManagerFactoryScanIntegrationTests.java

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
/*
* Copyright 2002-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jpa.eclipselink;
/**
* @author Juergen Hoeller
*/
class EclipseLinkEntityManagerFactoryScanIntegrationTests extends EclipseLinkEntityManagerFactoryIntegrationTests {
@Override
protected String[] getConfigLocations() {
return new String[] {"/org/springframework/orm/jpa/eclipselink/eclipselink-manager-scan.xml",
"/org/springframework/orm/jpa/memdb.xml", "/org/springframework/orm/jpa/inject.xml"};
}
}

45
spring-orm/src/test/java/org/springframework/orm/jpa/eclipselink/EclipseLinkEntityManagerFactorySimpleIntegrationTests.java

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
/*
* Copyright 2002-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jpa.eclipselink;
import org.junit.jupiter.api.Test;
import org.springframework.orm.jpa.EntityManagerFactoryInfo;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Juergen Hoeller
*/
class EclipseLinkEntityManagerFactorySimpleIntegrationTests extends EclipseLinkEntityManagerFactoryIntegrationTests {
@Override
protected String[] getConfigLocations() {
return new String[] {"/org/springframework/orm/jpa/eclipselink/eclipselink-manager-simple.xml",
"/org/springframework/orm/jpa/memdb.xml", "/org/springframework/orm/jpa/inject.xml"};
}
@Test
protected void testEntityManagerFactoryImplementsEntityManagerFactoryInfo() {
assertThat(entityManagerFactory).isInstanceOf(EntityManagerFactoryInfo.class);
EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) entityManagerFactory;
assertThat(emfi.getPersistenceUnitName()).isEqualTo("Person");
assertThat(emfi.getPersistenceUnitInfo()).as("PersistenceUnitInfo must not be used").isNull();
assertThat(emfi.getNativeEntityManagerFactory()).as("Raw EntityManagerFactory must be available").isNotNull();
}
}

30
spring-orm/src/test/java/org/springframework/orm/jpa/hibernate/HibernateEntityManagerFactoryScanIntegrationTests.java

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
/*
* Copyright 2002-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jpa.hibernate;
/**
* @author Juergen Hoeller
*/
class HibernateEntityManagerFactoryScanIntegrationTests extends HibernateEntityManagerFactoryIntegrationTests {
@Override
protected String[] getConfigLocations() {
return new String[] {"/org/springframework/orm/jpa/hibernate/hibernate-manager-scan.xml",
"/org/springframework/orm/jpa/memdb.xml", "/org/springframework/orm/jpa/inject.xml"};
}
}

45
spring-orm/src/test/java/org/springframework/orm/jpa/hibernate/HibernateEntityManagerFactorySimpleIntegrationTests.java

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
/*
* Copyright 2002-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jpa.hibernate;
import org.junit.jupiter.api.Test;
import org.springframework.orm.jpa.EntityManagerFactoryInfo;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Juergen Hoeller
*/
class HibernateEntityManagerFactorySimpleIntegrationTests extends HibernateEntityManagerFactoryIntegrationTests {
@Override
protected String[] getConfigLocations() {
return new String[] {"/org/springframework/orm/jpa/hibernate/hibernate-manager-simple.xml",
"/org/springframework/orm/jpa/memdb.xml", "/org/springframework/orm/jpa/inject.xml"};
}
@Test
protected void testEntityManagerFactoryImplementsEntityManagerFactoryInfo() {
assertThat(entityManagerFactory).isInstanceOf(EntityManagerFactoryInfo.class);
EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) entityManagerFactory;
assertThat(emfi.getPersistenceUnitName()).isEqualTo("Person");
assertThat(emfi.getPersistenceUnitInfo()).as("PersistenceUnitInfo must not be used").isNull();
assertThat(emfi.getNativeEntityManagerFactory()).as("Raw EntityManagerFactory must be available").isNotNull();
}
}

3
spring-orm/src/test/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesBeanRegistrationAotProcessorTests.java

@ -71,7 +71,8 @@ class PersistenceManagedTypesBeanRegistrationAotProcessorTests { @@ -71,7 +71,8 @@ class PersistenceManagedTypesBeanRegistrationAotProcessorTests {
PersistenceManagedTypes persistenceManagedTypes = freshApplicationContext.getBean(
"persistenceManagedTypes", PersistenceManagedTypes.class);
assertThat(persistenceManagedTypes.getManagedClassNames()).containsExactlyInAnyOrder(
DriversLicense.class.getName(), Person.class.getName(), Employee.class.getName(),
Person.class.getName(), DriversLicense.class.getName(), Employee.class.getName(),
EmployeeCategoryConverter.class.getName(), EmployeeKindConverter.class.getName(),
EmployeeLocationConverter.class.getName(), Car.class.getName());
assertThat(persistenceManagedTypes.getManagedPackages()).isEmpty();
assertThat(freshApplicationContext.getBean(

14
spring-orm/src/test/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesScannerTests.java

@ -26,6 +26,8 @@ import org.springframework.core.io.DefaultResourceLoader; @@ -26,6 +26,8 @@ import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.orm.jpa.domain.Car;
import org.springframework.orm.jpa.domain.DriversLicense;
import org.springframework.orm.jpa.domain.Employee;
import org.springframework.orm.jpa.domain.EmployeeCategoryConverter;
import org.springframework.orm.jpa.domain.EmployeeKindConverter;
import org.springframework.orm.jpa.domain.EmployeeLocationConverter;
import org.springframework.orm.jpa.domain.Person;
import org.springframework.orm.jpa.domain2.entity.User;
@ -44,15 +46,17 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; @@ -44,15 +46,17 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
*/
class PersistenceManagedTypesScannerTests {
public static final DefaultResourceLoader RESOURCE_LOADER = new DefaultResourceLoader();
private final DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
private final PersistenceManagedTypesScanner scanner = new PersistenceManagedTypesScanner(resourceLoader);
private final PersistenceManagedTypesScanner scanner = new PersistenceManagedTypesScanner(RESOURCE_LOADER);
@Test
void scanPackageWithOnlyEntities() {
PersistenceManagedTypes managedTypes = this.scanner.scan("org.springframework.orm.jpa.domain");
assertThat(managedTypes.getManagedClassNames()).containsExactlyInAnyOrder(
Person.class.getName(), DriversLicense.class.getName(), Employee.class.getName(),
EmployeeCategoryConverter.class.getName(), EmployeeKindConverter.class.getName(),
EmployeeLocationConverter.class.getName(), Car.class.getName());
assertThat(managedTypes.getManagedPackages()).isEmpty();
}
@ -61,11 +65,13 @@ class PersistenceManagedTypesScannerTests { @@ -61,11 +65,13 @@ class PersistenceManagedTypesScannerTests {
void scanPackageInvokesManagedClassNamesFilter() {
ManagedClassNameFilter filter = mock(ManagedClassNameFilter.class);
given(filter.matches(anyString())).willReturn(true);
new PersistenceManagedTypesScanner(RESOURCE_LOADER, filter)
new PersistenceManagedTypesScanner(resourceLoader, filter)
.scan("org.springframework.orm.jpa.domain");
verify(filter).matches(Person.class.getName());
verify(filter).matches(DriversLicense.class.getName());
verify(filter).matches(Employee.class.getName());
verify(filter).matches(EmployeeCategoryConverter.class.getName());
verify(filter).matches(EmployeeKindConverter.class.getName());
verify(filter).matches(EmployeeLocationConverter.class.getName());
verify(filter).matches(Car.class.getName());
verifyNoMoreInteractions(filter);
@ -75,7 +81,7 @@ class PersistenceManagedTypesScannerTests { @@ -75,7 +81,7 @@ class PersistenceManagedTypesScannerTests {
void scanPackageWithUseManagedClassNamesFilter() {
List<String> candidates = List.of(Person.class.getName(), DriversLicense.class.getName());
PersistenceManagedTypes managedTypes = new PersistenceManagedTypesScanner(
RESOURCE_LOADER, candidates::contains).scan("org.springframework.orm.jpa.domain");
resourceLoader, candidates::contains).scan("org.springframework.orm.jpa.domain");
assertThat(managedTypes.getManagedClassNames()).containsExactlyInAnyOrder(
Person.class.getName(), DriversLicense.class.getName());
assertThat(managedTypes.getManagedPackages()).isEmpty();

29
spring-orm/src/test/resources/org/springframework/orm/jpa/eclipselink/eclipselink-manager-scan.xml

@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<qualifier type="org.springframework.orm.jpa.domain.MyDomain"/>
<property name="persistenceUnitName" value="Person"/>
<property name="packagesToScan" value="org.springframework.orm.jpa.domain*"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<property name="database" value="HSQL"/>
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
</bean>
</property>
<property name="jpaPropertyMap">
<props>
<prop key="eclipselink.weaving">false</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
</beans>

29
spring-orm/src/test/resources/org/springframework/orm/jpa/eclipselink/eclipselink-manager-simple.xml

@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<qualifier type="org.springframework.orm.jpa.domain.MyDomain"/>
<property name="persistenceUnitName" value="Person"/>
<property name="packagesToScan" value="org.springframework.orm.jpa.domain*"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<property name="database" value="HSQL"/>
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
</bean>
</property>
<property name="jpaPropertyMap">
<props>
<prop key="eclipselink.weaving">false</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
</beans>

10
spring-orm/src/test/resources/org/springframework/orm/jpa/eclipselink/eclipselink-manager.xml

@ -1,14 +1,10 @@ @@ -1,14 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "https://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="org/springframework/orm/jpa/domain/persistence.xml"/>
<!--
<property name="persistenceUnitName" value="Person"/>
<property name="packagesToScan" value="org.springframework.orm.jpa.domain"/>
-->
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">

42
spring-orm/src/test/resources/org/springframework/orm/jpa/hibernate/hibernate-manager-scan.xml

@ -0,0 +1,42 @@ @@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" primary="true">
<qualifier type="org.springframework.orm.jpa.domain.MyDomain"/>
<property name="persistenceUnitName" value="Person"/>
<property name="packagesToScan" value="org.springframework.orm.jpa.domain*"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="HSQL"/>
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
</bean>
</property>
<property name="jpaPropertyMap">
<props>
<prop key="hibernate.current_session_context_class">org.springframework.orm.jpa.hibernate.SpringSessionContext</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
</props>
</property>
<property name="bootstrapExecutor">
<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="hibernateStatistics" factory-bean="entityManagerFactory" factory-method="getStatistics" lazy-init="true"/>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
<bean class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="pointcut"><bean class="org.springframework.orm.jpa.EntityManagerPointcut"/></property>
<property name="advice"><bean class="org.springframework.aop.interceptor.SimpleTraceInterceptor"/></property>
</bean>
</beans>

42
spring-orm/src/test/resources/org/springframework/orm/jpa/hibernate/hibernate-manager-simple.xml

@ -0,0 +1,42 @@ @@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" primary="true">
<qualifier type="org.springframework.orm.jpa.domain.MyDomain"/>
<property name="persistenceUnitName" value="Person"/>
<property name="packagesToScan" value="org.springframework.orm.jpa.domain*"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="HSQL"/>
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
</bean>
</property>
<property name="jpaPropertyMap">
<props>
<prop key="hibernate.current_session_context_class">org.springframework.orm.jpa.hibernate.SpringSessionContext</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
</props>
</property>
<property name="bootstrapExecutor">
<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="hibernateStatistics" factory-bean="entityManagerFactory" factory-method="getStatistics" lazy-init="true"/>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
<bean class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="pointcut"><bean class="org.springframework.orm.jpa.EntityManagerPointcut"/></property>
<property name="advice"><bean class="org.springframework.aop.interceptor.SimpleTraceInterceptor"/></property>
</bean>
</beans>
Loading…
Cancel
Save