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

@@ -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";
}
}