Skip to content

Commit 5334ac5

Browse files
jhoellerunknown
authored andcommitted
"packagesToScan" feature for Hibernate 3 and Hibernate 4 detects annotated packages as well
Issue: SPR-7748 Issue: SPR-10288
1 parent 39c6341 commit 5334ac5

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,6 +68,8 @@ public class LocalSessionFactoryBuilder extends Configuration {
6868

6969
private static final String RESOURCE_PATTERN = "/**/*.class";
7070

71+
private static final String PACKAGE_INFO_SUFFIX = ".package-info";
72+
7173
private static final TypeFilter[] ENTITY_TYPE_FILTERS = new TypeFilter[] {
7274
new AnnotationTypeFilter(Entity.class, false),
7375
new AnnotationTypeFilter(Embeddable.class, false),
@@ -194,8 +196,11 @@ public LocalSessionFactoryBuilder scanPackages(String... packagesToScan) throws
194196
if (resource.isReadable()) {
195197
MetadataReader reader = readerFactory.getMetadataReader(resource);
196198
String className = reader.getClassMetadata().getClassName();
197-
if (matchesFilter(reader, readerFactory)) {
198-
addAnnotatedClasses(this.resourcePatternResolver.getClassLoader().loadClass(className));
199+
if (matchesEntityTypeFilter(reader, readerFactory)) {
200+
addAnnotatedClass(this.resourcePatternResolver.getClassLoader().loadClass(className));
201+
}
202+
else if (className.endsWith(PACKAGE_INFO_SUFFIX)) {
203+
addPackage(className.substring(0, className.length() - PACKAGE_INFO_SUFFIX.length()));
199204
}
200205
}
201206
}
@@ -214,7 +219,7 @@ public LocalSessionFactoryBuilder scanPackages(String... packagesToScan) throws
214219
* Check whether any of the configured entity type filters matches
215220
* the current class descriptor contained in the metadata reader.
216221
*/
217-
private boolean matchesFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException {
222+
private boolean matchesEntityTypeFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException {
218223
for (TypeFilter filter : ENTITY_TYPE_FILTERS) {
219224
if (filter.match(reader, readerFactory)) {
220225
return true;

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -79,6 +79,8 @@ public class AnnotationSessionFactoryBean extends LocalSessionFactoryBean implem
7979

8080
private static final String RESOURCE_PATTERN = "/**/*.class";
8181

82+
private static final String PACKAGE_INFO_SUFFIX = ".package-info";
83+
8284

8385
private Class[] annotatedClasses;
8486

@@ -101,7 +103,7 @@ public AnnotationSessionFactoryBean() {
101103

102104

103105
@Override
104-
public void setConfigurationClass(Class configurationClass) {
106+
public void setConfigurationClass(Class<?> configurationClass) {
105107
if (configurationClass == null || !AnnotationConfiguration.class.isAssignableFrom(configurationClass)) {
106108
throw new IllegalArgumentException(
107109
"AnnotationSessionFactoryBean only supports AnnotationConfiguration or subclasses");
@@ -191,9 +193,12 @@ protected void scanPackages(AnnotationConfiguration config) {
191193
if (resource.isReadable()) {
192194
MetadataReader reader = readerFactory.getMetadataReader(resource);
193195
String className = reader.getClassMetadata().getClassName();
194-
if (matchesFilter(reader, readerFactory)) {
196+
if (matchesEntityTypeFilter(reader, readerFactory)) {
195197
config.addAnnotatedClass(this.resourcePatternResolver.getClassLoader().loadClass(className));
196198
}
199+
else if (className.endsWith(PACKAGE_INFO_SUFFIX)) {
200+
config.addPackage(className.substring(0, className.length() - PACKAGE_INFO_SUFFIX.length()));
201+
}
197202
}
198203
}
199204
}
@@ -211,7 +216,7 @@ protected void scanPackages(AnnotationConfiguration config) {
211216
* Check whether any of the configured entity type filters matches
212217
* the current class descriptor contained in the metadata reader.
213218
*/
214-
private boolean matchesFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException {
219+
private boolean matchesEntityTypeFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException {
215220
if (this.entityTypeFilters != null) {
216221
for (TypeFilter filter : this.entityTypeFilters) {
217222
if (filter.match(reader, readerFactory)) {

0 commit comments

Comments
 (0)