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 e98d92d96..9051ed439 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 @@ -20,7 +20,8 @@ import java.lang.reflect.Field; import java.lang.reflect.Member; import java.lang.reflect.Method; -import javax.persistence.metamodel.EntityType; +import javax.persistence.metamodel.IdentifiableType; +import javax.persistence.metamodel.ManagedType; import javax.persistence.metamodel.Metamodel; import javax.persistence.metamodel.SingularAttribute; @@ -49,13 +50,18 @@ public class JpaMetamodelEntityInformation extends J super(domainClass); Assert.notNull(metamodel); - EntityType type = metamodel.entity(domainClass); + ManagedType type = metamodel.managedType(domainClass); if (type == null) { throw new IllegalArgumentException("The given domain class can not be found in the given Metamodel!"); } - this.attribute = type.getId(type.getIdType().getJavaType()); + if (!(type instanceof IdentifiableType)) { + throw new IllegalArgumentException("The given domain class does not contain an id attribute!"); + } + + IdentifiableType identifiableType = (IdentifiableType) type; + this.attribute = identifiableType.getId(identifiableType.getIdType().getJavaType()); } /* diff --git a/src/test/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformationIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformationIntegrationTests.java new file mode 100644 index 000000000..27d125b67 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformationIntegrationTests.java @@ -0,0 +1,65 @@ +/* + * Copyright 2011 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.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +import java.io.Serializable; + +import javax.persistence.EntityManager; +import javax.persistence.MappedSuperclass; +import javax.persistence.PersistenceContext; +import javax.persistence.metamodel.Metamodel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.data.jpa.domain.AbstractPersistable; +import org.springframework.data.jpa.domain.sample.User; +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). + * + * @see https://hibernate.onjira.com/browse/HHH-6896 + * @author Oliver Gierke + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration({ "classpath:infrastructure.xml", "classpath:openjpa.xml" }) +public class JpaMetamodelEntityInformationIntegrationTests { + + @PersistenceContext + EntityManager em; + + @Test + public void detectsIdTypeForEntity() { + + JpaEntityInformation information = JpaEntityInformationSupport.getMetadata(User.class, em); + assertThat(information.getIdType(), is(typeCompatibleWith(Integer.class))); + } + + /** + * @see DATAJPA-141 + */ + @Test + public void detectsIdTypeForMappedSuperclass() { + + JpaEntityInformation information = JpaEntityInformationSupport.getMetadata(AbstractPersistable.class, em); + assertEquals(Serializable.class, information.getIdType()); + } +} diff --git a/src/test/java/org/springframework/data/jpa/repository/support/JpaPersistableEntityInformationUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/support/JpaPersistableEntityInformationUnitTests.java index ad694a515..0788e3a37 100644 --- a/src/test/java/org/springframework/data/jpa/repository/support/JpaPersistableEntityInformationUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/support/JpaPersistableEntityInformationUnitTests.java @@ -53,7 +53,7 @@ public class JpaPersistableEntityInformationUnitTests { @SuppressWarnings("unchecked") public void setUp() { - when(metamodel.entity(Foo.class)).thenReturn(type); + when(metamodel.managedType(Foo.class)).thenReturn(type); when(type.getIdType()).thenReturn(idType); }