DATAJPA-348 - Extending workaround for HHH-6951.

We're now using AnnotationUtils.findAnnotation(…) in case of Hibernate to improve workaround for HHH-6951. Added id type caching as the annotation lookup is potentially expensive.

Added integration tests for Hibernate and EclipseLink.
This commit is contained in:
Oliver Gierke
2013-06-20 12:06:42 +02:00
parent ba74ee10ba
commit 4dffef0943
7 changed files with 211 additions and 12 deletions

View File

@@ -33,6 +33,7 @@ import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.NotReadablePropertyException;
import org.springframework.beans.NotWritablePropertyException;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
@@ -197,6 +198,7 @@ public class JpaMetamodelEntityInformation<T, ID extends Serializable> extends J
private final IdentifiableType<T> type;
private final Set<SingularAttribute<? super T, ?>> attributes;
private Class<?> idType;
@SuppressWarnings("unchecked")
public IdMetadata(IdentifiableType<T> source) {
@@ -212,13 +214,22 @@ public class JpaMetamodelEntityInformation<T, ID extends Serializable> extends J
public Class<?> getType() {
if (idType != null) {
return idType;
}
Class<?> idType;
try {
return type.getIdType().getJavaType();
idType = type.getIdType().getJavaType();
} catch (IllegalStateException e) {
// see https://hibernate.onjira.com/browse/HHH-6951
IdClass annotation = type.getJavaType().getAnnotation(IdClass.class);
return annotation == null ? null : annotation.value();
IdClass annotation = AnnotationUtils.findAnnotation(type.getJavaType(), IdClass.class);
idType = annotation == null ? null : annotation.value();
}
this.idType = idType;
return idType;
}
public SingularAttribute<? super T, ?> getSimpleIdAttribute() {

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.repository.support;
import static org.junit.Assert.*;
import java.io.Serializable;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.data.jpa.domain.AbstractPersistable;
import org.springframework.test.context.ContextConfiguration;
/**
* EclipseLink execution for {@link JpaMetamodelEntityInformationIntegrationTests}.
*
* @author Oliver Gierke
*/
@ContextConfiguration("classpath:eclipselink.xml")
public class EclipseLinkJpaMetamodelEntityInformationIntegrationTests extends
JpaMetamodelEntityInformationIntegrationTests {
/**
* Re-activate test. Change to check for {@link String} as OpenJpa defaults {@link Serializable}s to {@link String}.
*/
@Test
public void reactivatedDetectsIdTypeForMappedSuperclass() {
JpaEntityInformation<?, ?> information = JpaEntityInformationSupport.getMetadata(AbstractPersistable.class, em);
assertEquals(String.class, information.getIdType());
}
/**
* Ignored due to https://bugs.eclipse.org/bugs/show_bug.cgi?id=411231.
*/
@Override
@Ignore
public void findsIdClassOnMappedSuperclass() {}
@Override
protected String getMetadadataPersitenceUnitName() {
return "metadata_el";
}
}

View File

@@ -20,11 +20,19 @@ import static org.junit.Assert.*;
import java.io.Serializable;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.MappedSuperclass;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.metamodel.Metamodel;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.jpa.domain.AbstractPersistable;
@@ -37,18 +45,15 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Integration tests for {@link JpaMetamodelEntityInformation}. Has to run with OpenJPA as Hibernate does not implement
* {@link Metamodel#managedType(Class)} correctly (does not consider {@link MappedSuperclass}es correctly).
* Integration tests for {@link JpaMetamodelEntityInformation}.
*
* @see https://hibernate.onjira.com/browse/HHH-6896
* @author Oliver Gierke
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:infrastructure.xml", "classpath:openjpa.xml" })
@ContextConfiguration({ "classpath:infrastructure.xml" })
public class JpaMetamodelEntityInformationIntegrationTests {
@PersistenceContext
EntityManager em;
@PersistenceContext EntityManager em;
@Test
public void detectsIdTypeForEntity() {
@@ -58,9 +63,14 @@ public class JpaMetamodelEntityInformationIntegrationTests {
}
/**
* Ignored for Hibernate as it does not implement {@link Metamodel#managedType(Class)} correctly (does not consider
* {@link MappedSuperclass}es correctly).
*
* @see https://hibernate.onjira.com/browse/HHH-6896
* @see DATAJPA-141
*/
@Test
@Ignore
public void detectsIdTypeForMappedSuperclass() {
JpaEntityInformation<?, ?> information = JpaEntityInformationSupport.getMetadata(AbstractPersistable.class, em);
@@ -112,4 +122,45 @@ public class JpaMetamodelEntityInformationIntegrationTests {
entity.setId(null);
assertThat(information.isNew(entity), is(false));
}
/**
* @see DATAJPA-348
*/
@Test
public void findsIdClassOnMappedSuperclass() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory(getMetadadataPersitenceUnitName());
EntityManager em = emf.createEntityManager();
EntityInformation<Sample, BaseIdClass> information = new JpaMetamodelEntityInformation<Sample, BaseIdClass>(
Sample.class, em.getMetamodel());
assertThat(information.getIdType(), is((Object) BaseIdClass.class));
}
protected String getMetadadataPersitenceUnitName() {
return "metadata";
}
@SuppressWarnings("serial")
public static class BaseIdClass implements Serializable {
Long id;
Long feedRunId;
}
@MappedSuperclass
@IdClass(BaseIdClass.class)
@Access(AccessType.FIELD)
public static abstract class Identifiable {
@Id Long id;
@Id Long feedRunId;
}
@Entity
@Access(AccessType.FIELD)
public static class Sample extends Identifiable {
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.repository.support;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
/**
* OpenJpa execution for {@link JpaMetamodelEntityInformationIntegrationTests}.
*
* @author Oliver Gierke
*/
@ContextConfiguration("classpath:openjpa.xml")
public class OpenJpaMetamodelEntityInformationIntegrationTests extends JpaMetamodelEntityInformationIntegrationTests {
/**
* Re-activate test.
*/
@Test
public void reactivatedDetectsIdTypeForMappedSuperclass() {
super.detectsIdTypeForMappedSuperclass();
}
/**
* Ignore as it fails with weird {@link NoClassDefFoundError}.
*/
@Override
@Ignore
public void findsIdClassOnMappedSuperclass() {}
@Override
protected String getMetadadataPersitenceUnitName() {
return "metadata_oj";
}
}

View File

@@ -35,4 +35,36 @@
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
<!-- Custom PUs for metadata tests -->
<persistence-unit name="metadata">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.springframework.data.jpa.domain.sample.User</class>
<class>org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformationIntegrationTests$Sample</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
</properties>
</persistence-unit>
<persistence-unit name="metadata_el">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>org.springframework.data.jpa.domain.sample.User</class>
<class>org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformationIntegrationTests$Sample</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
<persistence-unit name="metadata_oj">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>org.springframework.data.jpa.domain.sample.User</class>
<class>org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformationIntegrationTests$Sample</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="openjpa.jdbc.DBDictionary" value="hsql" />
<property name="openjpa.ConnectionDriverName" value="org.hsqldb.jdbcDriver" />
<property name="openjpa.ConnectionURL" value="jdbc:hsqldb:mem:test" />
<property name="openjpa.ConnectionUserName" value="sa" />
<property name="openjpa.ConnectionPassword" value="" />
</properties>
</persistence-unit>
</persistence>

View File

@@ -2,8 +2,8 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- EclipseLink vendor adaptor with workaround platform class for HSQL usage -->
<bean id="vendorAdaptor" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter" parent="abstractVendorAdaptor" />

View File

@@ -2,7 +2,7 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- EclipseLink vendor adaptor with workaround platform class for HSQL usage -->