diff --git a/src/main/java/org/springframework/data/envers/repository/support/EnversRevisionRepositoryFactoryBean.java b/src/main/java/org/springframework/data/envers/repository/support/EnversRevisionRepositoryFactoryBean.java index de86c92..a8d4a3f 100755 --- a/src/main/java/org/springframework/data/envers/repository/support/EnversRevisionRepositoryFactoryBean.java +++ b/src/main/java/org/springframework/data/envers/repository/support/EnversRevisionRepositoryFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2018 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. @@ -21,13 +21,8 @@ import javax.persistence.EntityManager; import org.hibernate.envers.DefaultRevisionEntity; import org.springframework.beans.factory.FactoryBean; -import org.springframework.core.GenericTypeResolver; -import org.springframework.data.jpa.repository.support.JpaEntityInformation; import org.springframework.data.jpa.repository.support.JpaRepositoryFactory; import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean; -import org.springframework.data.querydsl.QuerydslPredicateExecutor; -import org.springframework.data.querydsl.QuerydslUtils; -import org.springframework.data.repository.core.RepositoryInformation; import org.springframework.data.repository.core.RepositoryMetadata; import org.springframework.data.repository.core.support.RepositoryComposition.RepositoryFragments; import org.springframework.data.repository.core.support.RepositoryFactorySupport; @@ -76,14 +71,16 @@ public class EnversRevisionRepositoryFactoryBean> extends JpaRepositoryFactory { private final RevisionEntityInformation revisionEntityInformation; + private final EntityManager entityManager; /** * Creates a new {@link RevisionRepositoryFactory} using the given {@link EntityManager} and revision entity class. - * + * * @param entityManager must not be {@literal null}. * @param revisionEntityClass can be {@literal null}, will default to {@link DefaultRevisionEntity}. */ @@ -91,73 +88,26 @@ public class EnversRevisionRepositoryFactoryBean !it.equals(DefaultRevisionEntity.class))// . map(ReflectionRevisionEntityInformation::new) // .orElseGet(DefaultRevisionEntityInformation::new); } - /* - * (non-Javadoc) - * @see org.springframework.data.jpa.repository.support.JpaRepositoryFactory#getTargetRepository(org.springframework.data.repository.core.RepositoryInformation, javax.persistence.EntityManager) - */ @Override - @SuppressWarnings("unchecked") - protected EnversRevisionRepositoryImpl getTargetRepository(RepositoryInformation information, - EntityManager entityManager) { + protected RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata) { - JpaEntityInformation entityInformation = (JpaEntityInformation) getEntityInformation( - information.getDomainType()); + Object fragmentImplementation = getTargetRepositoryViaReflection( // + EnversRevisionRepositoryImpl.class, // + getEntityInformation(metadata.getDomainType()), // + revisionEntityInformation, // + entityManager // + ); - return new EnversRevisionRepositoryImpl(entityInformation, revisionEntityInformation, entityManager); - } - - /* - * (non-Javadoc) - * @see org.springframework.data.jpa.repository.support.JpaRepositoryFactory#getRepositoryBaseClass(org.springframework.data.repository.core.RepositoryMetadata) - */ - @Override - protected Class getRepositoryBaseClass(RepositoryMetadata metadata) { - if (isQueryDslExecutor(metadata.getRepositoryInterface())) { - return QueryDslWithEnversRevisionRepository.class; - } else { - return EnversRevisionRepositoryImpl.class; - } - } - - /** - * Returns whether the given repository interface requires a QueryDsl specific implementation to be chosen. - * - * @param repositoryInterface - * @return - */ - private boolean isQueryDslExecutor(Class repositoryInterface) { - return QuerydslUtils.QUERY_DSL_PRESENT && QuerydslPredicateExecutor.class.isAssignableFrom(repositoryInterface); - } - - /* - * (non-Javadoc) - * @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getRepository(java.lang.Class, org.springframework.data.repository.core.support.RepositoryComposition.RepositoryFragments) - */ - @Override - @SuppressWarnings("hiding") - public T getRepository(Class repositoryInterface, RepositoryFragments fragments) { - - if (RevisionRepository.class.isAssignableFrom(repositoryInterface)) { - - Class[] typeArguments = GenericTypeResolver.resolveTypeArguments(repositoryInterface, - RevisionRepository.class); - Class revisionNumberType = typeArguments[2]; - - 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)); - } - } - - return super.getRepository(repositoryInterface, fragments); + return RepositoryFragments // + .just(fragmentImplementation) // + .append(super.getRepositoryFragments(metadata)); } } } diff --git a/src/main/java/org/springframework/data/envers/repository/support/EnversRevisionRepositoryImpl.java b/src/main/java/org/springframework/data/envers/repository/support/EnversRevisionRepositoryImpl.java index 6680a80..d50c44f 100755 --- a/src/main/java/org/springframework/data/envers/repository/support/EnversRevisionRepositoryImpl.java +++ b/src/main/java/org/springframework/data/envers/repository/support/EnversRevisionRepositoryImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2018 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. @@ -38,11 +38,11 @@ import org.springframework.data.history.RevisionMetadata; import org.springframework.data.history.RevisionSort; import org.springframework.data.history.Revisions; import org.springframework.data.jpa.repository.support.JpaEntityInformation; -import org.springframework.data.jpa.repository.support.SimpleJpaRepository; import org.springframework.data.repository.core.EntityInformation; import org.springframework.data.repository.history.RevisionRepository; import org.springframework.data.repository.history.support.RevisionEntityInformation; import org.springframework.data.util.StreamUtils; +import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; /** @@ -53,7 +53,8 @@ import org.springframework.util.Assert; * @author Michael Igler * @author Jens Schauder */ -public class EnversRevisionRepositoryImpl> extends SimpleJpaRepository +@Transactional(readOnly = true) +public class EnversRevisionRepositoryImpl> implements RevisionRepository { private final EntityInformation entityInformation; @@ -71,8 +72,6 @@ public class EnversRevisionRepositoryImpl entityInformation, RevisionEntityInformation revisionEntityInformation, EntityManager entityManager) { - super(entityInformation, entityManager); - Assert.notNull(revisionEntityInformation, "RevisionEntityInformation must not be null!"); this.entityInformation = entityInformation; diff --git a/src/main/java/org/springframework/data/envers/repository/support/QueryDslWithEnversRevisionRepository.java b/src/main/java/org/springframework/data/envers/repository/support/QueryDslWithEnversRevisionRepository.java deleted file mode 100644 index 1031cc8..0000000 --- a/src/main/java/org/springframework/data/envers/repository/support/QueryDslWithEnversRevisionRepository.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright 2015 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.envers.repository.support; - -import java.io.Serializable; -import java.util.Optional; - -import javax.persistence.EntityManager; - -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.history.Revision; -import org.springframework.data.history.Revisions; -import org.springframework.data.jpa.repository.support.JpaEntityInformation; -import org.springframework.data.jpa.repository.support.QuerydslJpaRepository; -import org.springframework.data.repository.history.support.RevisionEntityInformation; - -/** - * Repository implementation for both {@link org.springframework.data.repository.history.RevisionRepository} and - * {@link org.springframework.data.querydsl.QuerydslPredicateExecutor} interfaces. - * - * @author Dmytro Iaroslavskyi - */ -public class QueryDslWithEnversRevisionRepository> - extends QuerydslJpaRepository implements EnversRevisionRepository { - - private final EnversRevisionRepositoryImpl delegateRepository; - - /** - * Creates a new {@link QueryDslWithEnversRevisionRepository} using the given {@link JpaEntityInformation}, - * {@link RevisionEntityInformation} and {@link EntityManager}. - * - * @param entityInformation must not be {@literal null}. - * @param revisionEntityInformation must not be {@literal null}. - * @param entityManager must not be {@literal null}. - */ - public QueryDslWithEnversRevisionRepository(JpaEntityInformation entityInformation, - RevisionEntityInformation revisionEntityInformation, EntityManager entityManager) { - super(entityInformation, entityManager); - this.delegateRepository = new EnversRevisionRepositoryImpl(entityInformation, revisionEntityInformation, - entityManager); - } - - @Override - public Optional> findLastChangeRevision(ID id) { - return delegateRepository.findLastChangeRevision(id); - } - - @Override - public Optional> findRevision(ID id, N revisionNumber) { - return delegateRepository.findRevision(id, revisionNumber); - } - - @Override - public Revisions findRevisions(ID id) { - return delegateRepository.findRevisions(id); - } - - @Override - public Page> findRevisions(ID id, Pageable pageable) { - return delegateRepository.findRevisions(id, pageable); - } - -} \ No newline at end of file