Skip to content

Commit a841923

Browse files
committed
Scan for JPA 2.1 Converter annotation as well
Issue: SPR-10799
1 parent f329140 commit a841923

2 files changed

Lines changed: 43 additions & 9 deletions

File tree

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package org.springframework.orm.hibernate4;
1818

1919
import java.io.IOException;
20+
import java.lang.annotation.Annotation;
21+
import java.util.LinkedHashSet;
22+
import java.util.Set;
2023
import javax.persistence.Embeddable;
2124
import javax.persistence.Entity;
2225
import javax.persistence.MappedSuperclass;
@@ -70,10 +73,25 @@ public class LocalSessionFactoryBuilder extends Configuration {
7073

7174
private static final String PACKAGE_INFO_SUFFIX = ".package-info";
7275

73-
private static final TypeFilter[] ENTITY_TYPE_FILTERS = new TypeFilter[] {
74-
new AnnotationTypeFilter(Entity.class, false),
75-
new AnnotationTypeFilter(Embeddable.class, false),
76-
new AnnotationTypeFilter(MappedSuperclass.class, false)};
76+
77+
private static final Set<TypeFilter> entityTypeFilters;
78+
79+
static {
80+
entityTypeFilters = new LinkedHashSet<TypeFilter>(4);
81+
entityTypeFilters.add(new AnnotationTypeFilter(Entity.class, false));
82+
entityTypeFilters.add(new AnnotationTypeFilter(Embeddable.class, false));
83+
entityTypeFilters.add(new AnnotationTypeFilter(MappedSuperclass.class, false));
84+
try {
85+
@SuppressWarnings("unchecked")
86+
Class<? extends Annotation> converterAnnotation = (Class<? extends Annotation>)
87+
LocalSessionFactoryBuilder.class.getClassLoader().loadClass("javax.persistence.Converter");
88+
entityTypeFilters.add(new AnnotationTypeFilter(converterAnnotation, false));
89+
}
90+
catch (ClassNotFoundException ex) {
91+
// JPA 2.1 API not available - Hibernate <4.3
92+
}
93+
}
94+
7795

7896
private final ResourcePatternResolver resourcePatternResolver;
7997

@@ -220,7 +238,7 @@ else if (className.endsWith(PACKAGE_INFO_SUFFIX)) {
220238
* the current class descriptor contained in the metadata reader.
221239
*/
222240
private boolean matchesEntityTypeFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException {
223-
for (TypeFilter filter : ENTITY_TYPE_FILTERS) {
241+
for (TypeFilter filter : entityTypeFilters) {
224242
if (filter.match(reader, readerFactory)) {
225243
return true;
226244
}

spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package org.springframework.orm.jpa.persistenceunit;
1818

1919
import java.io.IOException;
20+
import java.lang.annotation.Annotation;
2021
import java.net.URL;
2122
import java.util.HashMap;
2223
import java.util.HashSet;
24+
import java.util.LinkedHashSet;
2325
import java.util.LinkedList;
2426
import java.util.List;
2527
import java.util.Map;
@@ -93,10 +95,24 @@ public class DefaultPersistenceUnitManager
9395
private static final String ENTITY_CLASS_RESOURCE_PATTERN = "/**/*.class";
9496

9597

96-
private static final TypeFilter[] entityTypeFilters = new TypeFilter[] {
97-
new AnnotationTypeFilter(Entity.class, false),
98-
new AnnotationTypeFilter(Embeddable.class, false),
99-
new AnnotationTypeFilter(MappedSuperclass.class, false)};
98+
private static final Set<TypeFilter> entityTypeFilters;
99+
100+
static {
101+
entityTypeFilters = new LinkedHashSet<TypeFilter>(4);
102+
entityTypeFilters.add(new AnnotationTypeFilter(Entity.class, false));
103+
entityTypeFilters.add(new AnnotationTypeFilter(Embeddable.class, false));
104+
entityTypeFilters.add(new AnnotationTypeFilter(MappedSuperclass.class, false));
105+
try {
106+
@SuppressWarnings("unchecked")
107+
Class<? extends Annotation> converterAnnotation = (Class<? extends Annotation>)
108+
DefaultPersistenceUnitManager.class.getClassLoader().loadClass("javax.persistence.Converter");
109+
entityTypeFilters.add(new AnnotationTypeFilter(converterAnnotation, false));
110+
}
111+
catch (ClassNotFoundException ex) {
112+
// JPA 2.1 API not available
113+
}
114+
}
115+
100116

101117
private String[] persistenceXmlLocations = new String[] {DEFAULT_PERSISTENCE_XML_LOCATION};
102118

0 commit comments

Comments
 (0)