DATAJPA-19 - Adapted generics changes.
This commit is contained in:
@@ -109,7 +109,7 @@ public abstract class AbstractJpaQuery implements RepositoryQuery {
|
||||
case PAGING:
|
||||
return new PagedExecution(getParameters());
|
||||
case MODIFYING:
|
||||
EntityMetadata<?> metadata = method.getEntityMetadata();
|
||||
EntityMetadata<?> metadata = method.getEntityInformation();
|
||||
return method.getClearAutomatically() ? new ModifyingExecution(
|
||||
metadata, em) : new ModifyingExecution(metadata, null);
|
||||
default:
|
||||
|
||||
@@ -49,7 +49,7 @@ public class PartTreeJpaQuery extends AbstractJpaQuery {
|
||||
|
||||
super(method, em);
|
||||
this.tree =
|
||||
new PartTree(method.getName(), method.getEntityMetadata()
|
||||
new PartTree(method.getName(), method.getEntityInformation()
|
||||
.getJavaType());
|
||||
this.method = method;
|
||||
}
|
||||
@@ -68,7 +68,7 @@ public class PartTreeJpaQuery extends AbstractJpaQuery {
|
||||
ParameterAccessor accessor =
|
||||
new ParametersParameterAccessor(getParameters(), parameters);
|
||||
|
||||
EntityMetadata<?> metadata = method.getEntityMetadata();
|
||||
EntityMetadata<?> metadata = method.getEntityInformation();
|
||||
JpaQueryCreator jpaQueryCreator =
|
||||
new JpaQueryCreator(tree, accessor, metadata.getJavaType(),
|
||||
getEntityManager());
|
||||
@@ -97,8 +97,8 @@ public class PartTreeJpaQuery extends AbstractJpaQuery {
|
||||
CriteriaQuery<Object> query =
|
||||
new JpaCountQueryCreator(tree, new ParametersParameterAccessor(
|
||||
getParameters(), parameters), method
|
||||
.getEntityMetadata().getJavaType(), getEntityManager())
|
||||
.createQuery();
|
||||
.getEntityInformation().getJavaType(),
|
||||
getEntityManager()).createQuery();
|
||||
return getEntityManager().createQuery(query);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.jpa.repository.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.metamodel.SingularAttribute;
|
||||
|
||||
import org.springframework.data.repository.support.EntityInformation;
|
||||
@@ -26,7 +28,8 @@ import org.springframework.data.repository.support.EntityInformation;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface JpaEntityInformation<T> extends EntityInformation<T> {
|
||||
public interface JpaEntityInformation<T, ID extends Serializable> extends
|
||||
EntityInformation<T, ID> {
|
||||
|
||||
/**
|
||||
* Returns the id attribute of the entity.
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.jpa.repository.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Member;
|
||||
import java.lang.reflect.Method;
|
||||
@@ -35,8 +36,8 @@ import org.springframework.util.ReflectionUtils;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class JpaMetamodelEntityInformation<T> extends AbstractEntityInformation<T>
|
||||
implements JpaEntityInformation<T> {
|
||||
public class JpaMetamodelEntityInformation<T, ID extends Serializable> extends
|
||||
AbstractEntityInformation<T, ID> implements JpaEntityInformation<T, ID> {
|
||||
|
||||
private final SingularAttribute<? super T, ?> attribute;
|
||||
|
||||
@@ -48,7 +49,8 @@ public class JpaMetamodelEntityInformation<T> extends AbstractEntityInformation<
|
||||
* @param domainClass
|
||||
* @param metamodel
|
||||
*/
|
||||
public JpaMetamodelEntityInformation(Class<T> domainClass, Metamodel metamodel) {
|
||||
public JpaMetamodelEntityInformation(Class<T> domainClass,
|
||||
Metamodel metamodel) {
|
||||
|
||||
super(domainClass);
|
||||
|
||||
@@ -71,9 +73,23 @@ public class JpaMetamodelEntityInformation<T> extends AbstractEntityInformation<
|
||||
* org.springframework.data.repository.support.IdAware#getId(java.lang.Object
|
||||
* )
|
||||
*/
|
||||
public Object getId(T entity) {
|
||||
@SuppressWarnings("unchecked")
|
||||
public ID getId(T entity) {
|
||||
|
||||
return getMemberValue(attribute.getJavaMember(), entity);
|
||||
return (ID) getMemberValue(attribute.getJavaMember(), entity);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.repository.support.EntityInformation#getIdType()
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Class<ID> getIdType() {
|
||||
|
||||
return (Class<ID>) attribute.getJavaType();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -28,8 +28,8 @@ import org.springframework.data.domain.Persistable;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class JpaPersistableEntityInformation<T extends Persistable> extends
|
||||
JpaMetamodelEntityInformation<T> {
|
||||
public class JpaPersistableEntityInformation<T extends Persistable<ID>, ID extends Serializable>
|
||||
extends JpaMetamodelEntityInformation<T, ID> {
|
||||
|
||||
/**
|
||||
* Creates a new {@link JpaPersistableEntityInformation} for the given
|
||||
@@ -53,7 +53,7 @@ public class JpaPersistableEntityInformation<T extends Persistable> extends
|
||||
* #getId(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public Serializable getId(T entity) {
|
||||
public ID getId(T entity) {
|
||||
|
||||
return entity.getId();
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.jpa.repository.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy;
|
||||
@@ -76,21 +78,15 @@ public class JpaRepositoryFactory extends RepositoryFactorySupport {
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
protected Object getTargetRepository(RepositoryMetadata metadata,
|
||||
EntityManager entityManager) {
|
||||
protected <T, ID extends Serializable> Object getTargetRepository(
|
||||
RepositoryMetadata metadata, EntityManager entityManager) {
|
||||
|
||||
JpaEntityInformation<?> entityMetadata =
|
||||
getEntityMetadata(metadata.getDomainClass());
|
||||
JpaEntityInformation<T, ID> entityMetadata =
|
||||
getEntityInformation((Class<T>) metadata.getDomainClass());
|
||||
return new SimpleJpaRepository(entityMetadata, entityManager);
|
||||
}
|
||||
|
||||
|
||||
protected JpaEntityInformation<?> getEntityMetadata(Class<?> domainClass) {
|
||||
|
||||
return JpaClassUtils.getMetadata(domainClass, entityManager);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
@@ -118,4 +114,21 @@ public class JpaRepositoryFactory extends RepositoryFactorySupport {
|
||||
|
||||
return JpaQueryLookupStrategy.create(entityManager, key, extractor);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.repository.support.RepositoryFactorySupport#
|
||||
* getEntityInformation(java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T, ID extends Serializable> JpaEntityInformation<T, ID> getEntityInformation(
|
||||
Class<T> domainClass) {
|
||||
|
||||
return (JpaEntityInformation<T, ID>) JpaClassUtils.getMetadata(
|
||||
domainClass, entityManager);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.jpa.repository.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
@@ -33,8 +35,8 @@ import org.springframework.util.Assert;
|
||||
* @author Eberhard Wolff
|
||||
* @param <T> the type of the repository
|
||||
*/
|
||||
public class JpaRepositoryFactoryBean<T extends JpaRepository<?, ?>> extends
|
||||
TransactionalRepositoryFactoryBeanSupport<T> {
|
||||
public class JpaRepositoryFactoryBean<T extends JpaRepository<S, ID>, S, ID extends Serializable>
|
||||
extends TransactionalRepositoryFactoryBeanSupport<T, S, ID> {
|
||||
|
||||
private EntityManager entityManager;
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@ import static org.springframework.data.jpa.repository.query.QueryUtils.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
@@ -27,7 +26,6 @@ import javax.persistence.NoResultException;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Path;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
@@ -54,7 +52,7 @@ import org.springframework.util.Assert;
|
||||
public class SimpleJpaRepository<T, ID extends Serializable> implements
|
||||
JpaRepository<T, ID> {
|
||||
|
||||
private final JpaEntityInformation<T> entityInformation;
|
||||
private final JpaEntityInformation<T, ID> entityInformation;
|
||||
private final EntityManager em;
|
||||
private final PersistenceProvider provider;
|
||||
|
||||
@@ -66,7 +64,7 @@ public class SimpleJpaRepository<T, ID extends Serializable> implements
|
||||
* @param entityMetadata
|
||||
* @param entityManager
|
||||
*/
|
||||
public SimpleJpaRepository(JpaEntityInformation<T> entityMetadata,
|
||||
public SimpleJpaRepository(JpaEntityInformation<T, ID> entityMetadata,
|
||||
EntityManager entityManager) {
|
||||
|
||||
Assert.notNull(entityMetadata);
|
||||
|
||||
@@ -91,8 +91,8 @@ public abstract class JpaClassUtils {
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public static JpaEntityInformation<?> getMetadata(Class<?> domainClass,
|
||||
EntityManager em) {
|
||||
public static <T> JpaEntityInformation<T, ?> getMetadata(
|
||||
Class<T> domainClass, EntityManager em) {
|
||||
|
||||
Metamodel metamodel = em.getMetamodel();
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ public class QueryLookupStrategyTests {
|
||||
@Test
|
||||
public void assertUseDeclaredQuery() {
|
||||
|
||||
JpaRepositoryFactoryBean<?> factory =
|
||||
JpaRepositoryFactoryBean<?, ?, ?> factory =
|
||||
context.getBean("&roleRepository",
|
||||
JpaRepositoryFactoryBean.class);
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ public class CustomGenericJpaRepository<T, ID extends Serializable> extends
|
||||
* @param domainClass
|
||||
* @param entityManager
|
||||
*/
|
||||
public CustomGenericJpaRepository(JpaEntityInformation<T> metadata,
|
||||
public CustomGenericJpaRepository(JpaEntityInformation<T, ID> metadata,
|
||||
EntityManager entityManager) {
|
||||
|
||||
super(metadata, entityManager);
|
||||
|
||||
@@ -55,7 +55,7 @@ public class CustomGenericJpaRepositoryFactory extends JpaRepositoryFactory {
|
||||
protected Object getTargetRepository(RepositoryMetadata metadata,
|
||||
EntityManager em) {
|
||||
|
||||
JpaEntityInformation<Object> entityMetadata =
|
||||
JpaEntityInformation<Object, Serializable> entityMetadata =
|
||||
mock(JpaEntityInformation.class);
|
||||
when(entityMetadata.getJavaType()).thenReturn(
|
||||
(Class<Object>) metadata.getDomainClass());
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.jpa.repository.custom;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
@@ -28,8 +30,8 @@ import org.springframework.data.repository.support.RepositoryFactorySupport;
|
||||
* @author Gil Markham
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class CustomGenericJpaRepositoryFactoryBean<T extends JpaRepository<?, ?>>
|
||||
extends JpaRepositoryFactoryBean<T> {
|
||||
public class CustomGenericJpaRepositoryFactoryBean<T extends JpaRepository<Object, Serializable>>
|
||||
extends JpaRepositoryFactoryBean<T, Object, Serializable> {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
|
||||
@@ -19,6 +19,7 @@ import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -49,7 +50,7 @@ import org.springframework.data.repository.support.RepositoryFactorySupport;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class JpaRepositoryFactoryBeanUnitTests {
|
||||
|
||||
JpaRepositoryFactoryBean<SimpleSampleRepository> factoryBean;
|
||||
JpaRepositoryFactoryBean<SimpleSampleRepository, User, Integer> factoryBean;
|
||||
|
||||
@Mock
|
||||
EntityManager entityManager;
|
||||
@@ -79,7 +80,7 @@ public class JpaRepositoryFactoryBeanUnitTests {
|
||||
|
||||
// Setup standard factory configuration
|
||||
factoryBean =
|
||||
new DummyJpaRepositoryFactoryBean<SimpleSampleRepository>();
|
||||
new DummyJpaRepositoryFactoryBean<SimpleSampleRepository, User, Integer>();
|
||||
factoryBean.setRepositoryInterface(SimpleSampleRepository.class);
|
||||
factoryBean.setEntityManager(entityManager);
|
||||
}
|
||||
@@ -128,12 +129,13 @@ public class JpaRepositoryFactoryBeanUnitTests {
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void preventsUnsetRepositoryInterface() throws Exception {
|
||||
|
||||
factoryBean = new JpaRepositoryFactoryBean<SimpleSampleRepository>();
|
||||
factoryBean =
|
||||
new JpaRepositoryFactoryBean<SimpleSampleRepository, User, Integer>();
|
||||
factoryBean.afterPropertiesSet();
|
||||
}
|
||||
|
||||
private class DummyJpaRepositoryFactoryBean<T extends JpaRepository<?, ?>>
|
||||
extends JpaRepositoryFactoryBean<T> {
|
||||
private class DummyJpaRepositoryFactoryBean<T extends JpaRepository<S, ID>, S, ID extends Serializable>
|
||||
extends JpaRepositoryFactoryBean<T, S, ID> {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.data.jpa.repository.support;
|
||||
import static junit.framework.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
@@ -45,7 +46,7 @@ public class JpaRepositoryFactoryUnitTests {
|
||||
@Mock
|
||||
EntityManager entityManager;
|
||||
@Mock
|
||||
JpaEntityInformation<?> metadata;
|
||||
JpaEntityInformation<Object, Serializable> metadata;
|
||||
|
||||
|
||||
@Before
|
||||
@@ -55,11 +56,12 @@ public class JpaRepositoryFactoryUnitTests {
|
||||
factory = new JpaRepositoryFactory(entityManager) {
|
||||
|
||||
@Override
|
||||
protected JpaEntityInformation<?> getEntityMetadata(
|
||||
java.lang.Class<?> domainClass) {
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T, ID extends Serializable> JpaEntityInformation<T, ID> getEntityInformation(
|
||||
Class<T> domainClass) {
|
||||
|
||||
return metadata;
|
||||
}
|
||||
return (JpaEntityInformation<T, ID>) metadata;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user