From a2f6b4c5df7696ab5ccdc3266a3b70fbed76759e Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 9 Dec 2013 14:29:04 +0100 Subject: [PATCH] DATAJPA-430 - Tweaks to be compatible with Hibernate 4.3. Latest Hibernate 4.3 releases have changes some behavior and internals slightly. Adapted the test cases accordingly and added another guard in JpaMetamodelEntityInformation to adhere to the new behavior. Added build profile to be able to build against Hibernate 4.3. --- pom.xml | 10 ++++++++-- .../support/JpaMetamodelEntityInformation.java | 13 ++++++++++--- .../query/PartTreeJpaQueryIntegrationTests.java | 9 +++++++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 9952d69a4..3b19057d0 100644 --- a/pom.xml +++ b/pom.xml @@ -35,13 +35,19 @@ hibernate-41 - 4.1.11.Final + 4.1.12.Final hibernate-42 - 4.2.0.Final + 4.2.7.SP1 + + + + hibernate-43 + + 4.3.0.CR2 diff --git a/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java b/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java index febc902c4..c43d2c5e7 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java @@ -28,6 +28,7 @@ import javax.persistence.metamodel.IdentifiableType; import javax.persistence.metamodel.ManagedType; import javax.persistence.metamodel.Metamodel; import javax.persistence.metamodel.SingularAttribute; +import javax.persistence.metamodel.Type; import javax.persistence.metamodel.Type.PersistenceType; import org.springframework.beans.BeanWrapper; @@ -225,17 +226,23 @@ public class JpaMetamodelEntityInformation extends J Class idType; try { - idType = type.getIdType().getJavaType(); + Type idType2 = type.getIdType(); + idType = idType2 == null ? fallbackIdTypeLookup(type) : idType2.getJavaType(); } catch (IllegalStateException e) { // see https://hibernate.onjira.com/browse/HHH-6951 - IdClass annotation = AnnotationUtils.findAnnotation(type.getJavaType(), IdClass.class); - idType = annotation == null ? null : annotation.value(); + idType = fallbackIdTypeLookup(type); } this.idType = idType; return idType; } + private static Class fallbackIdTypeLookup(IdentifiableType type) { + + IdClass annotation = AnnotationUtils.findAnnotation(type.getJavaType(), IdClass.class); + return annotation == null ? null : annotation.value(); + } + public SingularAttribute getSimpleIdAttribute() { return attributes.iterator().next(); } diff --git a/src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java index bf3b72183..a6f16b997 100644 --- a/src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java @@ -32,6 +32,7 @@ import javax.persistence.PersistenceContext; import javax.persistence.Query; import javax.persistence.TemporalType; +import org.hibernate.Version; import org.hibernate.ejb.HibernateQuery; import org.junit.Rule; import org.junit.Test; @@ -105,12 +106,12 @@ public class PartTreeJpaQueryIntegrationTests { Query query = jpaQuery.createQuery(new Object[] { "Matthews", new PageRequest(0, 1) }); - HibernateQuery hibernateQuery = getValue(query, "h.target.val$jpaqlQuery"); + HibernateQuery hibernateQuery = getValue(query, "h.target." + (isHibernate43() ? "jpqlQuery" : "val$jpaqlQuery")); assertThat(hibernateQuery.getHibernateQuery().getQueryString(), endsWith("firstname=:param0")); query = jpaQuery.createQuery(new Object[] { null, new PageRequest(0, 1) }); - hibernateQuery = getValue(query, "h.target.val$jpaqlQuery"); + hibernateQuery = getValue(query, "h.target." + (isHibernate43() ? "jpqlQuery" : "val$jpaqlQuery")); assertThat(hibernateQuery.getHibernateQuery().getQueryString(), endsWith("firstname is null")); } @@ -140,6 +141,10 @@ public class PartTreeJpaQueryIntegrationTests { return (T) result; } + private static boolean isHibernate43() { + return Version.getVersionString().startsWith("4.3"); + } + interface UserRepository extends Repository { Page findByFirstname(String firstname, Pageable pageable);