Established well-defined registration order for Hibernate package scanning
Issue: SPR-7742
This commit is contained in:
parent
eb1b3c5a68
commit
92816492ed
|
|
@ -22,6 +22,7 @@ import java.lang.reflect.Method;
|
|||
import java.util.LinkedHashSet;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
|
|
@ -249,6 +250,8 @@ public class LocalSessionFactoryBuilder extends Configuration {
|
|||
* @throws HibernateException if scanning fails for any reason
|
||||
*/
|
||||
public LocalSessionFactoryBuilder scanPackages(String... packagesToScan) throws HibernateException {
|
||||
Set<String> classNames = new TreeSet<String>();
|
||||
Set<String> packageNames = new TreeSet<String>();
|
||||
try {
|
||||
for (String pkg : packagesToScan) {
|
||||
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
|
||||
|
|
@ -260,22 +263,30 @@ public class LocalSessionFactoryBuilder extends Configuration {
|
|||
MetadataReader reader = readerFactory.getMetadataReader(resource);
|
||||
String className = reader.getClassMetadata().getClassName();
|
||||
if (matchesEntityTypeFilter(reader, readerFactory)) {
|
||||
addAnnotatedClass(this.resourcePatternResolver.getClassLoader().loadClass(className));
|
||||
classNames.add(className);
|
||||
}
|
||||
else if (className.endsWith(PACKAGE_INFO_SUFFIX)) {
|
||||
addPackage(className.substring(0, className.length() - PACKAGE_INFO_SUFFIX.length()));
|
||||
packageNames.add(className.substring(0, className.length() - PACKAGE_INFO_SUFFIX.length()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new MappingException("Failed to scan classpath for unlisted classes", ex);
|
||||
}
|
||||
try {
|
||||
for (String className : classNames) {
|
||||
addAnnotatedClass(this.resourcePatternResolver.getClassLoader().loadClass(className));
|
||||
}
|
||||
for (String packageName : packageNames) {
|
||||
addPackage(packageName);
|
||||
}
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
throw new MappingException("Failed to load annotated classes from classpath", ex);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -17,7 +17,8 @@
|
|||
package org.springframework.orm.hibernate3.annotation;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
|
|
@ -25,6 +26,7 @@ import javax.persistence.MappedSuperclass;
|
|||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.MappingException;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
|
||||
import org.springframework.context.ResourceLoaderAware;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
|
|
@ -67,6 +69,8 @@ import org.springframework.util.ClassUtils;
|
|||
* <property name="packagesToScan" value="test.package"/>
|
||||
* </bean></pre>
|
||||
*
|
||||
* <p>Requires Hibernate 3.6 or later, as of Spring 4.0.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.2.2
|
||||
* @see #setDataSource
|
||||
|
|
@ -168,6 +172,8 @@ public class AnnotationSessionFactoryBean extends LocalSessionFactoryBean implem
|
|||
*/
|
||||
protected void scanPackages(Configuration config) {
|
||||
if (this.packagesToScan != null) {
|
||||
Set<String> classNames = new TreeSet<String>();
|
||||
Set<String> packageNames = new TreeSet<String>();
|
||||
try {
|
||||
for (String pkg : this.packagesToScan) {
|
||||
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
|
||||
|
|
@ -179,10 +185,10 @@ public class AnnotationSessionFactoryBean extends LocalSessionFactoryBean implem
|
|||
MetadataReader reader = readerFactory.getMetadataReader(resource);
|
||||
String className = reader.getClassMetadata().getClassName();
|
||||
if (matchesEntityTypeFilter(reader, readerFactory)) {
|
||||
config.addAnnotatedClass(this.resourcePatternResolver.getClassLoader().loadClass(className));
|
||||
classNames.add(className);
|
||||
}
|
||||
else if (className.endsWith(PACKAGE_INFO_SUFFIX)) {
|
||||
config.addPackage(className.substring(0, className.length() - PACKAGE_INFO_SUFFIX.length()));
|
||||
packageNames.add(className.substring(0, className.length() - PACKAGE_INFO_SUFFIX.length()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -191,6 +197,14 @@ public class AnnotationSessionFactoryBean extends LocalSessionFactoryBean implem
|
|||
catch (IOException ex) {
|
||||
throw new MappingException("Failed to scan classpath for unlisted classes", ex);
|
||||
}
|
||||
try {
|
||||
for (String className : classNames) {
|
||||
config.addAnnotatedClass(this.resourcePatternResolver.getClassLoader().loadClass(className));
|
||||
}
|
||||
for (String packageName : packageNames) {
|
||||
config.addPackage(packageName);
|
||||
}
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
throw new MappingException("Failed to load annotated classes from classpath", ex);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue