From a841923f122c4cdc21be122140dc456d736c6142 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 2 Aug 2013 11:08:30 +0200 Subject: [PATCH] Scan for JPA 2.1 Converter annotation as well Issue: SPR-10799 --- .../LocalSessionFactoryBuilder.java | 28 +++++++++++++++---- .../DefaultPersistenceUnitManager.java | 24 +++++++++++++--- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.java index b0aac2d2bb..859935bad9 100644 --- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.java +++ b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.java @@ -17,6 +17,9 @@ package org.springframework.orm.hibernate4; import java.io.IOException; +import java.lang.annotation.Annotation; +import java.util.LinkedHashSet; +import java.util.Set; import javax.persistence.Embeddable; import javax.persistence.Entity; import javax.persistence.MappedSuperclass; @@ -70,10 +73,25 @@ public class LocalSessionFactoryBuilder extends Configuration { private static final String PACKAGE_INFO_SUFFIX = ".package-info"; - private static final TypeFilter[] ENTITY_TYPE_FILTERS = new TypeFilter[] { - new AnnotationTypeFilter(Entity.class, false), - new AnnotationTypeFilter(Embeddable.class, false), - new AnnotationTypeFilter(MappedSuperclass.class, false)}; + + private static final Set entityTypeFilters; + + static { + entityTypeFilters = new LinkedHashSet(4); + entityTypeFilters.add(new AnnotationTypeFilter(Entity.class, false)); + entityTypeFilters.add(new AnnotationTypeFilter(Embeddable.class, false)); + entityTypeFilters.add(new AnnotationTypeFilter(MappedSuperclass.class, false)); + try { + @SuppressWarnings("unchecked") + Class converterAnnotation = (Class) + LocalSessionFactoryBuilder.class.getClassLoader().loadClass("javax.persistence.Converter"); + entityTypeFilters.add(new AnnotationTypeFilter(converterAnnotation, false)); + } + catch (ClassNotFoundException ex) { + // JPA 2.1 API not available - Hibernate <4.3 + } + } + private final ResourcePatternResolver resourcePatternResolver; @@ -220,7 +238,7 @@ public class LocalSessionFactoryBuilder extends Configuration { * the current class descriptor contained in the metadata reader. */ private boolean matchesEntityTypeFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException { - for (TypeFilter filter : ENTITY_TYPE_FILTERS) { + for (TypeFilter filter : entityTypeFilters) { if (filter.match(reader, readerFactory)) { return true; } diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java index 3a0fe03f28..e150f63c11 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java @@ -17,9 +17,11 @@ package org.springframework.orm.jpa.persistenceunit; import java.io.IOException; +import java.lang.annotation.Annotation; import java.net.URL; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -93,10 +95,24 @@ public class DefaultPersistenceUnitManager private static final String ENTITY_CLASS_RESOURCE_PATTERN = "/**/*.class"; - private static final TypeFilter[] entityTypeFilters = new TypeFilter[] { - new AnnotationTypeFilter(Entity.class, false), - new AnnotationTypeFilter(Embeddable.class, false), - new AnnotationTypeFilter(MappedSuperclass.class, false)}; + private static final Set entityTypeFilters; + + static { + entityTypeFilters = new LinkedHashSet(4); + entityTypeFilters.add(new AnnotationTypeFilter(Entity.class, false)); + entityTypeFilters.add(new AnnotationTypeFilter(Embeddable.class, false)); + entityTypeFilters.add(new AnnotationTypeFilter(MappedSuperclass.class, false)); + try { + @SuppressWarnings("unchecked") + Class converterAnnotation = (Class) + DefaultPersistenceUnitManager.class.getClassLoader().loadClass("javax.persistence.Converter"); + entityTypeFilters.add(new AnnotationTypeFilter(converterAnnotation, false)); + } + catch (ClassNotFoundException ex) { + // JPA 2.1 API not available + } + } + private String[] persistenceXmlLocations = new String[] {DEFAULT_PERSISTENCE_XML_LOCATION};