DATAJPA-141 - JpaMetamodelEntityInformation now works with MappedSuperclasses as well.

We now call Metamodel.managedClass(…) instead of Metamodel.entityType(…) to be able to detect ids in @MappedSuperclass types as well. Unfortunately this currently still fails with Hibernate as theit Metamodel implementation only considers entities. See [0] for further details.

[0] https://hibernate.onjira.com/browse/HHH-6896
This commit is contained in:
Oliver Gierke
2011-12-15 11:30:13 +01:00
parent fe36a77f38
commit ea8fff00fc
3 changed files with 75 additions and 4 deletions

View File

@@ -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<T, ID extends Serializable> extends J
super(domainClass);
Assert.notNull(metamodel);
EntityType<T> type = metamodel.entity(domainClass);
ManagedType<T> 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<T> identifiableType = (IdentifiableType<T>) type;
this.attribute = identifiableType.getId(identifiableType.getIdType().getJavaType());
}
/*

View File

@@ -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<User, ?> 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());
}
}

View File

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