#66 - Adapt API in EnversRepositoryFactoryBean.

Related tickets: DATACMNS-891, DATACMNS-960.
This commit is contained in:
Oliver Gierke
2016-12-15 15:41:14 +01:00
parent c4f3c01deb
commit aa732d2752
3 changed files with 30 additions and 26 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
@@ -17,7 +17,6 @@ package org.springframework.data.envers.repository.support;
import java.io.Serializable;
import org.springframework.data.history.Revision;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.history.RevisionRepository;
@@ -28,17 +27,11 @@ import org.springframework.data.repository.history.RevisionRepository;
*
* @author Oliver Gierke
* @author Michael Igler
* @deprecated since 1.1, in favor of simply extending {@link RevisionRepository}.
*/
@Deprecated
@NoRepositoryBean
public interface EnversRevisionRepository<T, ID extends Serializable, N extends Number & Comparable<N>> extends
RevisionRepository<T, ID, N>, JpaRepository<T, ID> {
public interface EnversRevisionRepository<T, ID extends Serializable, N extends Number & Comparable<N>>
extends RevisionRepository<T, ID, N>, JpaRepository<T, ID> {
/**
* Returns the entity with the given ID in the given revision number.
*
* @param id must not be {@literal null}.
* @param revisionNumber must not be {@literal null}.
* @return
*/
Revision<N, T> findRevision(ID id, N revisionNumber);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2016 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.
@@ -37,11 +37,20 @@ import org.springframework.data.repository.history.support.RevisionEntityInforma
* @author Oliver Gierke
* @author Michael Igler
*/
public class EnversRevisionRepositoryFactoryBean extends
JpaRepositoryFactoryBean<EnversRevisionRepository<Object, Serializable, Long>, Object, Serializable> {
public class EnversRevisionRepositoryFactoryBean<T extends RevisionRepository<S, ID, N>, S, ID extends Serializable, N extends Number & Comparable<N>>
extends JpaRepositoryFactoryBean<T, S, ID> {
private Class<?> revisionEntityClass;
/**
* Creates a new {@link EnversRevisionRepositoryFactoryBean} for the given repository interface.
*
* @param repositoryInterface must not be {@literal null}.
*/
public EnversRevisionRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
super(repositoryInterface);
}
/**
* Configures the revision entity class. Will default to {@link DefaultRevisionEntity}.
*
@@ -57,7 +66,7 @@ public class EnversRevisionRepositoryFactoryBean extends
*/
@Override
protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
return new RevisionRepositoryFactory(entityManager, revisionEntityClass);
return new RevisionRepositoryFactory<T, ID, N>(entityManager, revisionEntityClass);
}
/**
@@ -65,7 +74,8 @@ public class EnversRevisionRepositoryFactoryBean extends
*
* @author Oliver Gierke
*/
private static class RevisionRepositoryFactory<T, ID extends Serializable, N extends Number & Comparable<N>> extends JpaRepositoryFactory {
private static class RevisionRepositoryFactory<T, ID extends Serializable, N extends Number & Comparable<N>>
extends JpaRepositoryFactory {
private final RevisionEntityInformation revisionEntityInformation;
private final EntityManager entityManager;
@@ -81,8 +91,8 @@ public class EnversRevisionRepositoryFactoryBean extends
super(entityManager);
this.entityManager = entityManager;
revisionEntityClass = revisionEntityClass == null ? DefaultRevisionEntity.class : revisionEntityClass;
this.revisionEntityInformation = DefaultRevisionEntity.class.equals(revisionEntityClass) ? new DefaultRevisionEntityInformation()
: new ReflectionRevisionEntityInformation(revisionEntityClass);
this.revisionEntityInformation = DefaultRevisionEntity.class.equals(revisionEntityClass)
? new DefaultRevisionEntityInformation() : new ReflectionRevisionEntityInformation(revisionEntityClass);
}
/*
@@ -93,9 +103,10 @@ public class EnversRevisionRepositoryFactoryBean extends
@SuppressWarnings({ "unchecked", "rawtypes" })
protected EnversRevisionRepositoryImpl getTargetRepository(RepositoryInformation information) {
JpaEntityInformation<T, Serializable> entityInformation = (JpaEntityInformation<T, Serializable>) getEntityInformation(information.getDomainType());
JpaEntityInformation<T, Serializable> entityInformation = (JpaEntityInformation<T, Serializable>) getEntityInformation(
information.getDomainType());
return new EnversRevisionRepositoryImpl<T, ID, N>(entityInformation , revisionEntityInformation, entityManager);
return new EnversRevisionRepositoryImpl<T, ID, N>(entityInformation, revisionEntityInformation, entityManager);
}
/*
@@ -112,6 +123,7 @@ public class EnversRevisionRepositoryFactoryBean extends
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getRepository(java.lang.Class, java.lang.Object)
*/
@Override
@SuppressWarnings("hiding")
public <T> T getRepository(Class<T> repositoryInterface, Object customImplementation) {
if (RevisionRepository.class.isAssignableFrom(repositoryInterface)) {
@@ -123,8 +135,8 @@ public class EnversRevisionRepositoryFactoryBean extends
if (!revisionEntityInformation.getRevisionNumberType().equals(revisionNumberType)) {
throw new IllegalStateException(String.format(
"Configured a revision entity type of %s with a revision type of %s "
+ "but the repository interface is typed to a revision type of %s!", repositoryInterface,
revisionEntityInformation.getRevisionNumberType(), revisionNumberType));
+ "but the repository interface is typed to a revision type of %s!",
repositoryInterface, revisionEntityInformation.getRevisionNumberType(), revisionNumberType));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2016 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.
@@ -15,7 +15,6 @@
*/
package org.springframework.data.envers.sample;
import org.springframework.data.envers.repository.support.EnversRevisionRepository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.history.RevisionRepository;
@@ -24,6 +23,6 @@ import org.springframework.data.repository.history.RevisionRepository;
*
* @author Oliver Gierke
*/
public interface CountryRepository extends EnversRevisionRepository<Country, Long, Integer>, JpaRepository<Country, Long> {
public interface CountryRepository extends RevisionRepository<Country, Long, Integer>, JpaRepository<Country, Long> {
}