Browse Source

Established well-defined registration order for Hibernate package scanning

Issue: SPR-7742
pull/424/merge
Juergen Hoeller 11 years ago
parent
commit
92816492ed
  1. 17
      spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.java
  2. 20
      spring-orm/src/main/java/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.java

17
spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.java

@ -22,6 +22,7 @@ import java.lang.reflect.Method; @@ -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 { @@ -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 { @@ -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;
}
/**

20
spring-orm/src/main/java/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.java

@ -17,7 +17,8 @@ @@ -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; @@ -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; @@ -67,6 +69,8 @@ import org.springframework.util.ClassUtils;
* &lt;property name="packagesToScan" value="test.package"/&gt;
* &lt;/bean&gt;</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 @@ -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 @@ -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 @@ -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…
Cancel
Save