Merge the Envers repo.
Spring Data JPA is now a multimodule project. The artifacts build are still separate, except for the documentation which is now a single one. Closes # 2316
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
/*
|
||||
* Copyright 2021 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
|
||||
*
|
||||
* https://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.config;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.context.annotation.ComponentScan.Filter;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
|
||||
import org.springframework.data.repository.config.BootstrapMode;
|
||||
import org.springframework.data.repository.config.DefaultRepositoryBaseClass;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
/**
|
||||
* Annotation to enable Envers repositories. Will scan the package of the annotated configuration class for Spring Data
|
||||
* repositories by default.
|
||||
* <p>
|
||||
* This annotation is a meta-annotation for {@link EnableJpaRepositories @EnableJpaRepositories} overriding the default
|
||||
* {@link #repositoryFactoryBeanClass} to {@link EnversRevisionRepositoryFactoryBean}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @since 2.5
|
||||
* @see EnableJpaRepositories
|
||||
* @see AliasFor
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
@EnableJpaRepositories
|
||||
public @interface EnableEnversRepositories {
|
||||
|
||||
/**
|
||||
* Alias for the {@link #basePackages()} attribute. Allows for more concise annotation declarations e.g.:
|
||||
* {@code @EnableJpaRepositories("org.my.pkg")} instead of
|
||||
* {@code @EnableEnversRepositories(basePackages="org.my.pkg")}.
|
||||
*/
|
||||
@AliasFor(annotation = EnableJpaRepositories.class)
|
||||
String[] value() default {};
|
||||
|
||||
/**
|
||||
* Base packages to scan for annotated components. {@link #value()} is an alias for (and mutually exclusive with) this
|
||||
* attribute. Use {@link #basePackageClasses()} for a type-safe alternative to String-based package names.
|
||||
*/
|
||||
@AliasFor(annotation = EnableJpaRepositories.class)
|
||||
String[] basePackages() default {};
|
||||
|
||||
/**
|
||||
* Type-safe alternative to {@link #basePackages()} for specifying the packages to scan for annotated components. The
|
||||
* package of each class specified will be scanned. Consider creating a special no-op marker class or interface in
|
||||
* each package that serves no purpose other than being referenced by this attribute.
|
||||
*/
|
||||
@AliasFor(annotation = EnableJpaRepositories.class)
|
||||
Class<?>[] basePackageClasses() default {};
|
||||
|
||||
/**
|
||||
* Specifies which types are eligible for component scanning. Further narrows the set of candidate components from
|
||||
* everything in {@link #basePackages()} to everything in the base packages that matches the given filter or filters.
|
||||
*/
|
||||
@AliasFor(annotation = EnableJpaRepositories.class)
|
||||
Filter[] includeFilters() default {};
|
||||
|
||||
/**
|
||||
* Specifies which types are not eligible for component scanning.
|
||||
*/
|
||||
@AliasFor(annotation = EnableJpaRepositories.class)
|
||||
Filter[] excludeFilters() default {};
|
||||
|
||||
/**
|
||||
* Returns the postfix to be used when looking up custom repository implementations. Defaults to {@literal Impl}. So
|
||||
* for a repository named {@code PersonRepository} the corresponding implementation class will be looked up scanning
|
||||
* for {@code PersonRepositoryImpl}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@AliasFor(annotation = EnableJpaRepositories.class)
|
||||
String repositoryImplementationPostfix() default "Impl";
|
||||
|
||||
/**
|
||||
* Configures the location of where to find the Spring Data named queries properties file. Will default to
|
||||
* {@code META-INF/jpa-named-queries.properties}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@AliasFor(annotation = EnableJpaRepositories.class)
|
||||
String namedQueriesLocation() default "";
|
||||
|
||||
/**
|
||||
* Returns the key of the {@link QueryLookupStrategy} to be used for lookup queries for query methods. Defaults to
|
||||
* {@link Key#CREATE_IF_NOT_FOUND}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@AliasFor(annotation = EnableJpaRepositories.class)
|
||||
Key queryLookupStrategy() default Key.CREATE_IF_NOT_FOUND;
|
||||
|
||||
/**
|
||||
* Returns the {@link FactoryBean} class to be used for each repository instance. Defaults to
|
||||
* {@link JpaRepositoryFactoryBean}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@AliasFor(annotation = EnableJpaRepositories.class)
|
||||
Class<?> repositoryFactoryBeanClass() default EnversRevisionRepositoryFactoryBean.class;
|
||||
|
||||
/**
|
||||
* Configure the repository base class to be used to create repository proxies for this particular configuration.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@AliasFor(annotation = EnableJpaRepositories.class)
|
||||
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;
|
||||
|
||||
// JPA specific configuration
|
||||
|
||||
/**
|
||||
* Configures the name of the {@link EntityManagerFactory} bean definition to be used to create repositories
|
||||
* discovered through this annotation. Defaults to {@code entityManagerFactory}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@AliasFor(annotation = EnableJpaRepositories.class)
|
||||
String entityManagerFactoryRef() default "entityManagerFactory";
|
||||
|
||||
/**
|
||||
* Configures the name of the {@link PlatformTransactionManager} bean definition to be used to create repositories
|
||||
* discovered through this annotation. Defaults to {@code transactionManager}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@AliasFor(annotation = EnableJpaRepositories.class)
|
||||
String transactionManagerRef() default "transactionManager";
|
||||
|
||||
/**
|
||||
* Configures whether nested repository-interfaces (e.g. defined as inner classes) should be discovered by the
|
||||
* repositories infrastructure.
|
||||
*/
|
||||
@AliasFor(annotation = EnableJpaRepositories.class)
|
||||
boolean considerNestedRepositories() default false;
|
||||
|
||||
/**
|
||||
* Configures whether to enable default transactions for Spring Data JPA repositories. Defaults to {@literal true}. If
|
||||
* disabled, repositories must be used behind a facade that's configuring transactions (e.g. using Spring's annotation
|
||||
* driven transaction facilities) or repository methods have to be used to demarcate transactions.
|
||||
*
|
||||
* @return whether to enable default transactions, defaults to {@literal true}.
|
||||
*/
|
||||
@AliasFor(annotation = EnableJpaRepositories.class)
|
||||
boolean enableDefaultTransactions() default true;
|
||||
|
||||
/**
|
||||
* Configures when the repositories are initialized in the bootstrap lifecycle. {@link BootstrapMode#DEFAULT}
|
||||
* (default) means eager initialization except all repository interfaces annotated with {@link Lazy},
|
||||
* {@link BootstrapMode#LAZY} means lazy by default including injection of lazy-initialization proxies into client
|
||||
* beans so that those can be instantiated but will only trigger the initialization upon first repository usage (i.e a
|
||||
* method invocation on it). This means repositories can still be uninitialized when the application context has
|
||||
* completed its bootstrap. {@link BootstrapMode#DEFERRED} is fundamentally the same as {@link BootstrapMode#LAZY},
|
||||
* but triggers repository initialization when the application context finishes its bootstrap.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@AliasFor(annotation = EnableJpaRepositories.class)
|
||||
BootstrapMode bootstrapMode() default BootstrapMode.DEFAULT;
|
||||
|
||||
/**
|
||||
* Configures what character is used to escape the wildcards {@literal _} and {@literal %} in derived queries with
|
||||
* {@literal contains}, {@literal startsWith} or {@literal endsWith} clauses.
|
||||
*
|
||||
* @return a single character used for escaping.
|
||||
*/
|
||||
@AliasFor(annotation = EnableJpaRepositories.class)
|
||||
char escapeCharacter() default '\\';
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* Classes for Envers Repositories configuration support.
|
||||
*/
|
||||
@org.springframework.lang.NonNullApi
|
||||
package org.springframework.data.envers.repository.config;
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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
|
||||
*
|
||||
* https://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 org.hibernate.envers.DefaultRevisionEntity;
|
||||
import org.springframework.data.repository.history.support.RevisionEntityInformation;
|
||||
|
||||
/**
|
||||
* {@link RevisionEntityInformation} for {@link DefaultRevisionEntity}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
class DefaultRevisionEntityInformation implements RevisionEntityInformation {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.history.support.RevisionEntityInformation#getRevisionNumberType()
|
||||
*/
|
||||
public Class<?> getRevisionNumberType() {
|
||||
return Integer.class;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.history.support.RevisionEntityInformation#isDefaultRevisionEntity()
|
||||
*/
|
||||
public boolean isDefaultRevisionEntity() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.history.support.RevisionEntityInformation#getRevisionEntityClass()
|
||||
*/
|
||||
public Class<?> getRevisionEntityClass() {
|
||||
return DefaultRevisionEntity.class;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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
|
||||
*
|
||||
* https://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.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.hibernate.envers.DefaultRevisionEntity;
|
||||
|
||||
import org.springframework.data.history.RevisionMetadata;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link RevisionMetadata} working with a {@link DefaultRevisionEntity}. The entity/delegate itself gets ignored for
|
||||
* {@link #equals(Object)} and {@link #hashCode()} since they depend on the way they were obtained.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Philip Huegelmeyer
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
public final class DefaultRevisionMetadata implements RevisionMetadata<Integer> {
|
||||
|
||||
private final DefaultRevisionEntity entity;
|
||||
private final RevisionType revisionType;
|
||||
|
||||
public DefaultRevisionMetadata(DefaultRevisionEntity entity) {
|
||||
this(entity, RevisionType.UNKNOWN);
|
||||
}
|
||||
|
||||
public DefaultRevisionMetadata(DefaultRevisionEntity entity, RevisionType revisionType) {
|
||||
|
||||
Assert.notNull(entity, "DefaultRevisionEntity must not be null");
|
||||
|
||||
this.entity = entity;
|
||||
this.revisionType = revisionType;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.history.RevisionMetadata#getRevisionNumber()
|
||||
*/
|
||||
public Optional<Integer> getRevisionNumber() {
|
||||
return Optional.of(entity.getId());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.history.RevisionMetadata#getRevisionDate()
|
||||
*/
|
||||
@Deprecated
|
||||
public Optional<LocalDateTime> getRevisionDate() {
|
||||
return getRevisionInstant().map(instant -> LocalDateTime.ofInstant(instant, ZoneOffset.systemDefault()));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.history.RevisionMetadata#getRevisionInstant()
|
||||
*/
|
||||
@Override
|
||||
public Optional<Instant> getRevisionInstant() {
|
||||
return Optional.of(Instant.ofEpochMilli(entity.getTimestamp()));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.history.RevisionMetadata#getDelegate()
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getDelegate() {
|
||||
return (T) entity;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.history.RevisionMetadata#getRevisionType()
|
||||
*/
|
||||
@Override
|
||||
public RevisionType getRevisionType() {
|
||||
return revisionType;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
DefaultRevisionMetadata that = (DefaultRevisionMetadata) o;
|
||||
return getRevisionNumber().equals(that.getRevisionNumber())
|
||||
&& getRevisionInstant().equals(that.getRevisionInstant()) && revisionType.equals(that.getRevisionType());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DefaultRevisionMetadata{" + "entity=" + entity + ", revisionType=" + revisionType + '}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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
|
||||
*
|
||||
* https://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 org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.repository.NoRepositoryBean;
|
||||
import org.springframework.data.repository.history.RevisionRepository;
|
||||
|
||||
/**
|
||||
* Convenience interface to allow pulling in {@link JpaRepository} and {@link RevisionRepository} functionality in one
|
||||
* go.
|
||||
*
|
||||
* @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> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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
|
||||
*
|
||||
* https://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.util.Optional;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
|
||||
import org.hibernate.envers.DefaultRevisionEntity;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
|
||||
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.RepositoryComposition.RepositoryFragments;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
import org.springframework.data.repository.history.RevisionRepository;
|
||||
import org.springframework.data.repository.history.support.RevisionEntityInformation;
|
||||
|
||||
/**
|
||||
* {@link FactoryBean} creating {@link RevisionRepository} instances.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Michael Igler
|
||||
*/
|
||||
public class EnversRevisionRepositoryFactoryBean<T extends RevisionRepository<S, ID, N>, S, ID, 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}.
|
||||
*
|
||||
* @param revisionEntityClass
|
||||
*/
|
||||
public void setRevisionEntityClass(Class<?> revisionEntityClass) {
|
||||
this.revisionEntityClass = revisionEntityClass;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean#createRepositoryFactory(jakarta.persistence.EntityManager)
|
||||
*/
|
||||
@Override
|
||||
protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
|
||||
return new RevisionRepositoryFactory<T, ID, N>(entityManager, revisionEntityClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Repository factory creating {@link RevisionRepository} instances.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
private static class RevisionRepositoryFactory<T, ID, N extends Number & Comparable<N>> 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}.
|
||||
*/
|
||||
public RevisionRepositoryFactory(EntityManager entityManager, Class<?> revisionEntityClass) {
|
||||
|
||||
super(entityManager);
|
||||
|
||||
this.entityManager = entityManager;
|
||||
this.revisionEntityInformation = Optional.ofNullable(revisionEntityClass) //
|
||||
.filter(it -> !it.equals(DefaultRevisionEntity.class))//
|
||||
.<RevisionEntityInformation> map(ReflectionRevisionEntityInformation::new) //
|
||||
.orElseGet(DefaultRevisionEntityInformation::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata) {
|
||||
|
||||
Object fragmentImplementation = getTargetRepositoryViaReflection( //
|
||||
EnversRevisionRepositoryImpl.class, //
|
||||
getEntityInformation(metadata.getDomainType()), //
|
||||
revisionEntityInformation, //
|
||||
entityManager //
|
||||
);
|
||||
|
||||
return RepositoryFragments //
|
||||
.just(fragmentImplementation) //
|
||||
.append(super.getRepositoryFragments(metadata));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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
|
||||
*
|
||||
* https://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 static org.springframework.data.history.RevisionMetadata.RevisionType.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
|
||||
import org.hibernate.envers.AuditReader;
|
||||
import org.hibernate.envers.AuditReaderFactory;
|
||||
import org.hibernate.envers.DefaultRevisionEntity;
|
||||
import org.hibernate.envers.RevisionNumber;
|
||||
import org.hibernate.envers.RevisionTimestamp;
|
||||
import org.hibernate.envers.RevisionType;
|
||||
import org.hibernate.envers.query.AuditEntity;
|
||||
import org.hibernate.envers.query.AuditQuery;
|
||||
import org.hibernate.envers.query.order.AuditOrder;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.history.AnnotationRevisionMetadata;
|
||||
import org.springframework.data.history.Revision;
|
||||
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.repository.core.EntityInformation;
|
||||
import org.springframework.data.repository.history.RevisionRepository;
|
||||
import org.springframework.data.repository.history.support.RevisionEntityInformation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Repository implementation using Hibernate Envers to implement revision specific query methods.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Philipp Huegelmeyer
|
||||
* @author Michael Igler
|
||||
* @author Jens Schauder
|
||||
* @author Julien Millau
|
||||
* @author Mark Paluch
|
||||
* @author Sander Bylemans
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public class EnversRevisionRepositoryImpl<T, ID, N extends Number & Comparable<N>>
|
||||
implements RevisionRepository<T, ID, N> {
|
||||
|
||||
private final EntityInformation<T, ?> entityInformation;
|
||||
private final EntityManager entityManager;
|
||||
|
||||
/**
|
||||
* Creates a new {@link EnversRevisionRepositoryImpl} 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 EnversRevisionRepositoryImpl(JpaEntityInformation<T, ?> entityInformation,
|
||||
RevisionEntityInformation revisionEntityInformation, EntityManager entityManager) {
|
||||
|
||||
Assert.notNull(revisionEntityInformation, "RevisionEntityInformation must not be null!");
|
||||
|
||||
this.entityInformation = entityInformation;
|
||||
this.entityManager = entityManager;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.history.RevisionRepository#findLastChangeRevision(java.io.Serializable)
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Optional<Revision<N, T>> findLastChangeRevision(ID id) {
|
||||
|
||||
List<Object[]> singleResult = createBaseQuery(id) //
|
||||
.addOrder(AuditEntity.revisionProperty("timestamp").desc()) //
|
||||
.setMaxResults(1) //
|
||||
.getResultList();
|
||||
|
||||
Assert.state(singleResult.size() <= 1, "We expect at most one result.");
|
||||
|
||||
if (singleResult.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return Optional.of(createRevision(new QueryResult<>(singleResult.get(0))));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.envers.repository.support.EnversRevisionRepository#findRevision(java.io.Serializable, java.lang.Number)
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Optional<Revision<N, T>> findRevision(ID id, N revisionNumber) {
|
||||
|
||||
Assert.notNull(id, "Identifier must not be null!");
|
||||
Assert.notNull(revisionNumber, "Revision number must not be null!");
|
||||
|
||||
List<Object[]> singleResult = (List<Object[]>) createBaseQuery(id) //
|
||||
.add(AuditEntity.revisionNumber().eq(revisionNumber)) //
|
||||
.getResultList();
|
||||
|
||||
Assert.state(singleResult.size() <= 1, "We expect at most one result.");
|
||||
|
||||
if (singleResult.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return Optional.of(createRevision(new QueryResult<>(singleResult.get(0))));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Revisions<N, T> findRevisions(ID id) {
|
||||
|
||||
List<Object[]> resultList = createBaseQuery(id).getResultList();
|
||||
List<Revision<N, T>> revisionList = new ArrayList<>(resultList.size());
|
||||
|
||||
for (Object[] objects : resultList) {
|
||||
revisionList.add(createRevision(new QueryResult<>(objects)));
|
||||
}
|
||||
|
||||
return Revisions.of(revisionList);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Page<Revision<N, T>> findRevisions(ID id, Pageable pageable) {
|
||||
|
||||
AuditOrder sorting = RevisionSort.getRevisionDirection(pageable.getSort()).isDescending() //
|
||||
? AuditEntity.revisionNumber().desc() //
|
||||
: AuditEntity.revisionNumber().asc();
|
||||
|
||||
List<Object[]> resultList = createBaseQuery(id) //
|
||||
.addOrder(sorting) //
|
||||
.setFirstResult((int) pageable.getOffset()) //
|
||||
.setMaxResults(pageable.getPageSize()) //
|
||||
.getResultList();
|
||||
|
||||
Long count = (Long) createBaseQuery(id) //
|
||||
.addProjection(AuditEntity.revisionNumber().count()).getSingleResult();
|
||||
|
||||
List<Revision<N, T>> revisions = new ArrayList<>();
|
||||
|
||||
for (Object[] singleResult : resultList) {
|
||||
revisions.add(createRevision(new QueryResult<>(singleResult)));
|
||||
}
|
||||
|
||||
return new PageImpl<>(revisions, pageable, count);
|
||||
}
|
||||
|
||||
private AuditQuery createBaseQuery(ID id) {
|
||||
|
||||
Class<T> type = entityInformation.getJavaType();
|
||||
AuditReader reader = AuditReaderFactory.get(entityManager);
|
||||
|
||||
return reader.createQuery() //
|
||||
.forRevisionsOfEntity(type, false, true) //
|
||||
.add(AuditEntity.id().eq(id));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Revision<N, T> createRevision(QueryResult<T> queryResult) {
|
||||
return Revision.of((RevisionMetadata<N>) queryResult.createRevisionMetadata(), queryResult.entity);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static class QueryResult<T> {
|
||||
|
||||
private final T entity;
|
||||
private final Object metadata;
|
||||
private final RevisionMetadata.RevisionType revisionType;
|
||||
|
||||
QueryResult(Object[] data) {
|
||||
|
||||
Assert.notNull(data, "Data must not be null");
|
||||
Assert.isTrue( //
|
||||
data.length == 3, //
|
||||
() -> String.format("Data must have length three, but has length %d.", data.length));
|
||||
Assert.isTrue( //
|
||||
data[2] instanceof RevisionType, //
|
||||
() -> String.format("The third array element must be of type Revision type, but is of type %s",
|
||||
data[2].getClass()));
|
||||
|
||||
entity = (T) data[0];
|
||||
metadata = data[1];
|
||||
revisionType = convertRevisionType((RevisionType) data[2]);
|
||||
}
|
||||
|
||||
RevisionMetadata<?> createRevisionMetadata() {
|
||||
|
||||
return metadata instanceof DefaultRevisionEntity //
|
||||
? new DefaultRevisionMetadata((DefaultRevisionEntity) metadata, revisionType) //
|
||||
: new AnnotationRevisionMetadata<>(metadata, RevisionNumber.class, RevisionTimestamp.class, revisionType);
|
||||
}
|
||||
|
||||
private static RevisionMetadata.RevisionType convertRevisionType(RevisionType datum) {
|
||||
|
||||
switch (datum) {
|
||||
|
||||
case ADD:
|
||||
return INSERT;
|
||||
case MOD:
|
||||
return UPDATE;
|
||||
case DEL:
|
||||
return DELETE;
|
||||
default:
|
||||
return UNKNOWN;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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
|
||||
*
|
||||
* https://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 org.hibernate.envers.RevisionNumber;
|
||||
|
||||
import org.springframework.data.repository.history.support.RevisionEntityInformation;
|
||||
import org.springframework.data.util.AnnotationDetectionFieldCallback;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* {@link RevisionEntityInformation} that uses reflection to inspect a property annotated with {@link RevisionNumber} to
|
||||
* find out about the revision number type.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class ReflectionRevisionEntityInformation implements RevisionEntityInformation {
|
||||
|
||||
private final Class<?> revisionEntityClass;
|
||||
private final Class<?> revisionNumberType;
|
||||
|
||||
/**
|
||||
* Creates a new {@link ReflectionRevisionEntityInformation} inspecting the given revision entity class.
|
||||
*
|
||||
* @param revisionEntityClass must not be {@literal null}.
|
||||
*/
|
||||
public ReflectionRevisionEntityInformation(Class<?> revisionEntityClass) {
|
||||
|
||||
Assert.notNull(revisionEntityClass, "Revision entity type must not be null!");
|
||||
|
||||
AnnotationDetectionFieldCallback fieldCallback = new AnnotationDetectionFieldCallback(RevisionNumber.class);
|
||||
ReflectionUtils.doWithFields(revisionEntityClass, fieldCallback);
|
||||
|
||||
this.revisionNumberType = fieldCallback.getRequiredType();
|
||||
this.revisionEntityClass = revisionEntityClass;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.history.support.RevisionEntityInformation#isDefaultRevisionEntity()
|
||||
*/
|
||||
public boolean isDefaultRevisionEntity() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Class<?> getRevisionEntityClass() {
|
||||
return this.revisionEntityClass;
|
||||
}
|
||||
|
||||
public Class<?> getRevisionNumberType() {
|
||||
return this.revisionNumberType;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* Spring Data JPA specific converter infrastructure.
|
||||
*/
|
||||
@org.springframework.lang.NonNullApi
|
||||
package org.springframework.data.envers.repository.support;
|
||||
260
spring-data-envers/src/main/resources/changelog.txt
Normal file
260
spring-data-envers/src/main/resources/changelog.txt
Normal file
@@ -0,0 +1,260 @@
|
||||
Spring Data Envers Changelog
|
||||
==========================
|
||||
|
||||
Changes in version 2.6.0-M1 (2021-07-16)
|
||||
----------------------------------------
|
||||
* #305 - Upgrade to Envers 5.5.3.Final.
|
||||
* #288 - Update CI to Java 16.
|
||||
|
||||
|
||||
Changes in version 2.4.11 (2021-07-16)
|
||||
--------------------------------------
|
||||
|
||||
|
||||
Changes in version 2.5.2 (2021-06-22)
|
||||
-------------------------------------
|
||||
|
||||
|
||||
Changes in version 2.4.10 (2021-06-22)
|
||||
--------------------------------------
|
||||
|
||||
|
||||
Changes in version 2.5.1 (2021-05-14)
|
||||
-------------------------------------
|
||||
|
||||
|
||||
Changes in version 2.4.9 (2021-05-14)
|
||||
-------------------------------------
|
||||
|
||||
|
||||
Changes in version 2.5.0 (2021-04-14)
|
||||
-------------------------------------
|
||||
|
||||
|
||||
Changes in version 2.4.8 (2021-04-14)
|
||||
-------------------------------------
|
||||
|
||||
|
||||
Changes in version 2.3.9.RELEASE (2021-04-14)
|
||||
---------------------------------------------
|
||||
|
||||
|
||||
Changes in version 2.4.7 (2021-03-31)
|
||||
-------------------------------------
|
||||
|
||||
|
||||
Changes in version 2.5.0-RC1 (2021-03-31)
|
||||
-----------------------------------------
|
||||
* #289 - Investigate `@EnableEnversRepositories` meta-annotation.
|
||||
|
||||
|
||||
Changes in version 2.5.0-M5 (2021-03-17)
|
||||
----------------------------------------
|
||||
* #283 - Editing pass.
|
||||
* #282 - Add Java config example, fix StackOverflow tag URL.
|
||||
* #61 - Spring Data Envers Documentation Only Contains General Spring Data Information.
|
||||
|
||||
|
||||
Changes in version 2.4.6 (2021-03-17)
|
||||
-------------------------------------
|
||||
* #283 - Editing pass.
|
||||
* #282 - Add Java config example, fix StackOverflow tag URL.
|
||||
* #61 - Spring Data Envers Documentation Only Contains General Spring Data Information.
|
||||
|
||||
|
||||
Changes in version 2.3.8.RELEASE (2021-03-17)
|
||||
---------------------------------------------
|
||||
* #283 - Editing pass.
|
||||
* #282 - Add Java config example, fix StackOverflow tag URL.
|
||||
* #61 - Spring Data Envers Documentation Only Contains General Spring Data Information.
|
||||
|
||||
|
||||
Changes in version 2.5.0-M4 (2021-02-18)
|
||||
----------------------------------------
|
||||
|
||||
|
||||
Changes in version 2.4.5 (2021-02-18)
|
||||
-------------------------------------
|
||||
|
||||
|
||||
Changes in version 2.5.0-M3 (2021-02-17)
|
||||
----------------------------------------
|
||||
* #265 - Enable Project automation through GitHub Actions.
|
||||
|
||||
|
||||
Changes in version 2.4.4 (2021-02-17)
|
||||
-------------------------------------
|
||||
|
||||
|
||||
Changes in version 2.3.7.RELEASE (2021-02-17)
|
||||
---------------------------------------------
|
||||
* #271 - Update copyright year to 2021.
|
||||
* #266 - Update CI jobs with Docker Login.
|
||||
|
||||
|
||||
Changes in version 2.5.0-M2 (2021-01-13)
|
||||
----------------------------------------
|
||||
* #271 - Update copyright year to 2021.
|
||||
* #266 - Update CI jobs with Docker Login.
|
||||
|
||||
|
||||
Changes in version 2.4.3 (2021-01-13)
|
||||
-------------------------------------
|
||||
* #271 - Update copyright year to 2021.
|
||||
* #266 - Update CI jobs with Docker Login.
|
||||
|
||||
|
||||
Changes in version 2.4.2 (2020-12-09)
|
||||
-------------------------------------
|
||||
* #263 - Release 2.4.2 (2020.0.2).
|
||||
|
||||
|
||||
Changes in version 2.5.0-M1 (2020-12-09)
|
||||
----------------------------------------
|
||||
* #265 - Enable Project automation through GitHub Actions.
|
||||
* #262 - Release 2.5 M1 (2021.0.0).
|
||||
|
||||
|
||||
Changes in version 2.3.6.RELEASE (2020-12-09)
|
||||
---------------------------------------------
|
||||
* #260 - Release 2.3.6 (Neumann SR6).
|
||||
|
||||
|
||||
Changes in version 2.4.1 (2020-11-11)
|
||||
-------------------------------------
|
||||
* #261 - Release 2.4.1 (2020.0.1).
|
||||
|
||||
|
||||
Changes in version 2.4.0 (2020-10-28)
|
||||
-------------------------------------
|
||||
* #257 - Release 2.4 GA (2020.0.0).
|
||||
|
||||
|
||||
Changes in version 2.3.5.RELEASE (2020-10-28)
|
||||
---------------------------------------------
|
||||
* #254 - Release 2.3.5 (Neumann SR5).
|
||||
|
||||
|
||||
Changes in version 2.4.0-RC2 (2020-10-14)
|
||||
-----------------------------------------
|
||||
* #256 - Update CI jobs for Java 15.
|
||||
* #255 - Release 2.4 RC2 (2020.0.0).
|
||||
|
||||
|
||||
Changes in version 2.4.0-RC1 (2020-09-16)
|
||||
-----------------------------------------
|
||||
* #251 - Release 2.4 RC1 (2020.0.0).
|
||||
|
||||
|
||||
Changes in version 2.3.4.RELEASE (2020-09-16)
|
||||
---------------------------------------------
|
||||
* #252 - Release 2.3.4 (Neumann SR4).
|
||||
|
||||
|
||||
Changes in version 2.3.3.RELEASE (2020-08-12)
|
||||
---------------------------------------------
|
||||
* #247 - Release 2.3.3 (Neumann SR3).
|
||||
|
||||
|
||||
Changes in version 2.4.0-M2 (2020-08-12)
|
||||
----------------------------------------
|
||||
* #244 - Release 2.4 M2 (2020.0.0).
|
||||
|
||||
|
||||
Changes in version 2.3.2.RELEASE (2020-07-22)
|
||||
---------------------------------------------
|
||||
* #242 - Release 2.3.2 (Neumann SR2).
|
||||
|
||||
|
||||
Changes in version 2.4.0-M1 (2020-06-25)
|
||||
----------------------------------------
|
||||
* #239 - Use standard Spring code of conduct.
|
||||
* #238 - Delombok source files.
|
||||
* #237 - Release 2.4 M1 (2020.0.0).
|
||||
|
||||
|
||||
Changes in version 2.3.1.RELEASE (2020-06-10)
|
||||
---------------------------------------------
|
||||
* #236 - Release 2.3.1 (Neumann SR1).
|
||||
|
||||
|
||||
Changes in version 2.3.0.RELEASE (2020-05-12)
|
||||
---------------------------------------------
|
||||
* #234 - Release 2.3 GA (Neumann).
|
||||
|
||||
|
||||
Changes in version 2.3.0.RC2 (2020-04-28)
|
||||
-----------------------------------------
|
||||
* #230 - Use JDK 14 for Java.NEXT CI testing.
|
||||
* #229 - Release 2.3 RC2 (Neumann).
|
||||
* #215 - RevisionType always Unknown in RevisionMetadata.
|
||||
|
||||
|
||||
Changes in version 2.3.0.RC1 (2020-03-31)
|
||||
-----------------------------------------
|
||||
* #224 - Release 2.3 RC1 (Neumann).
|
||||
|
||||
|
||||
Changes in version 2.3.0.M4 (2020-03-11)
|
||||
----------------------------------------
|
||||
* #221 - Release 2.3 M4 (Neumann).
|
||||
|
||||
|
||||
Changes in version 2.3.0.M3 (2020-02-12)
|
||||
----------------------------------------
|
||||
* #220 - Release 2.3 M3 (Neumann).
|
||||
|
||||
|
||||
Changes in version 2.3.0.M2 (2020-01-17)
|
||||
----------------------------------------
|
||||
* #219 - Release 2.3 M2 (Neumann).
|
||||
|
||||
|
||||
Changes in version 2.3.0.M1 (2020-01-16)
|
||||
----------------------------------------
|
||||
* #210 - Upgrade to Envers 5.4.8.
|
||||
* #205 - Release 2.3 M1 (Neumann).
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
279
spring-data-envers/src/main/resources/license.txt
Normal file
279
spring-data-envers/src/main/resources/license.txt
Normal file
@@ -0,0 +1,279 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
https://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
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
|
||||
|
||||
https://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.
|
||||
|
||||
=======================================================================
|
||||
|
||||
SPRING FRAMEWORK ${version} SUBCOMPONENTS:
|
||||
|
||||
Spring Framework ${version} includes a number of subcomponents
|
||||
with separate copyright notices and license terms. The product that
|
||||
includes this file does not necessarily use all the open source
|
||||
subcomponents referred to below. Your use of the source
|
||||
code for these subcomponents is subject to the terms and
|
||||
conditions of the following licenses.
|
||||
|
||||
|
||||
>>> ASM 4.0 (org.ow2.asm:asm:4.0, org.ow2.asm:asm-commons:4.0):
|
||||
|
||||
Copyright (c) 2000-2011 INRIA, France Telecom
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of the copyright holders nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
Copyright (c) 1999-2009, OW2 Consortium <https://www.ow2.org/>
|
||||
|
||||
|
||||
>>> CGLIB 3.0 (cglib:cglib:3.0):
|
||||
|
||||
Per the LICENSE file in the CGLIB JAR distribution downloaded from
|
||||
https://sourceforge.net/projects/cglib/files/cglib3/3.0/cglib-3.0.jar/download,
|
||||
CGLIB 3.0 is licensed under the Apache License, version 2.0, the text of which
|
||||
is included above.
|
||||
|
||||
|
||||
=======================================================================
|
||||
|
||||
To the extent any open source subcomponents are licensed under the EPL and/or
|
||||
other similar licenses that require the source code and/or modifications to
|
||||
source code to be made available (as would be noted above), you may obtain a
|
||||
copy of the source code corresponding to the binaries for such open source
|
||||
components and modifications thereto, if any, (the "Source Files"), by
|
||||
downloading the Source Files from https://www.springsource.org/download, or by
|
||||
sending a request, with your name and address to:
|
||||
|
||||
VMware, Inc., 3401 Hillview Avenue
|
||||
Palo Alto, CA 94304
|
||||
United States of America
|
||||
|
||||
or email info@vmware.com. All such requests should clearly specify:
|
||||
|
||||
OPEN SOURCE FILES REQUEST
|
||||
Attention General Counsel
|
||||
|
||||
VMware shall mail a copy of the Source Files to you on a CD or equivalent
|
||||
physical medium. This offer to obtain a copy of the Source Files is valid for
|
||||
three years from the date you acquired this Software product.
|
||||
33
spring-data-envers/src/main/resources/notice.txt
Normal file
33
spring-data-envers/src/main/resources/notice.txt
Normal file
@@ -0,0 +1,33 @@
|
||||
Spring Data Envers 2.6 M3 (2021.1.0)
|
||||
Copyright (c) 2015-2019 Pivotal Software, Inc.
|
||||
|
||||
This product is licensed to you under the Apache License, Version 2.0
|
||||
(the "License"). You may not use this product except in compliance with
|
||||
the License.
|
||||
|
||||
This product may include a number of subcomponents with separate
|
||||
copyright notices and license terms. Your use of the source code for
|
||||
these subcomponents is subject to the terms and conditions of the
|
||||
subcomponent's license, as noted in the license.txt file.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
76
spring-data-envers/src/test/java/org/springframework/data/envers/Config.java
Executable file
76
spring-data-envers/src/test/java/org/springframework/data/envers/Config.java
Executable file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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
|
||||
*
|
||||
* https://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;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.hibernate.envers.strategy.ValidityAuditStrategy;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.envers.repository.config.EnableEnversRepositories;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.Database;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
/**
|
||||
* Spring JavaConfig configuration for general infrastructure.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Configuration
|
||||
@EnableEnversRepositories
|
||||
public class Config {
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() throws SQLException {
|
||||
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager transactionManager() throws SQLException {
|
||||
return new JpaTransactionManager();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AbstractEntityManagerFactoryBean entityManagerFactory() throws SQLException {
|
||||
|
||||
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
|
||||
jpaVendorAdapter.setDatabase(Database.H2);
|
||||
jpaVendorAdapter.setGenerateDdl(true);
|
||||
|
||||
LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
|
||||
bean.setJpaVendorAdapter(jpaVendorAdapter);
|
||||
bean.setPackagesToScan(Config.class.getPackage().getName());
|
||||
bean.setDataSource(dataSource());
|
||||
bean.setJpaPropertyMap(jpaProperties());
|
||||
|
||||
return bean;
|
||||
}
|
||||
|
||||
private Map<String, String> jpaProperties() {
|
||||
return Collections.singletonMap("org.hibernate.envers.audit_strategy", ValidityAuditStrategy.class.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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
|
||||
*
|
||||
* https://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 static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import org.hibernate.envers.DefaultRevisionEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link DefaultRevisionMetadata}.
|
||||
*
|
||||
* @author Benedikt Ritter
|
||||
* @author Jens Schauder
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class DefaultRevisionMetadataUnitTests {
|
||||
|
||||
private static final Instant NOW = Instant.now();;
|
||||
|
||||
@Test // #112
|
||||
void createsLocalDateTimeFromTimestamp() {
|
||||
|
||||
DefaultRevisionEntity entity = new DefaultRevisionEntity();
|
||||
entity.setTimestamp(NOW.toEpochMilli());
|
||||
|
||||
DefaultRevisionMetadata metadata = new DefaultRevisionMetadata(entity);
|
||||
|
||||
assertThat(metadata.getRevisionDate())
|
||||
.hasValue(LocalDateTime.ofInstant(NOW, ZoneOffset.systemDefault()).truncatedTo(ChronoUnit.MILLIS));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2020-2021 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
|
||||
*
|
||||
* https://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 static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.hibernate.envers.DefaultRevisionEntity;
|
||||
import org.hibernate.envers.RevisionType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.history.AnnotationRevisionMetadata;
|
||||
import org.springframework.data.history.RevisionMetadata;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link EnversRevisionRepositoryImpl}.
|
||||
*
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
class EnversRevisionRepositoryImplUnitTests {
|
||||
|
||||
@Test // gh-215
|
||||
void revisionTypeOfAnnotationRevisionMetadataIsProperlySet() {
|
||||
|
||||
Object[] data = new Object[] { "a", "some metadata", RevisionType.DEL };
|
||||
|
||||
EnversRevisionRepositoryImpl.QueryResult<Object> result = new EnversRevisionRepositoryImpl.QueryResult<>(data);
|
||||
|
||||
RevisionMetadata<?> revisionMetadata = result.createRevisionMetadata();
|
||||
|
||||
assertThat(revisionMetadata).isInstanceOf(AnnotationRevisionMetadata.class);
|
||||
assertThat(revisionMetadata.getRevisionType()).isEqualTo(RevisionMetadata.RevisionType.DELETE);
|
||||
}
|
||||
|
||||
@Test // gh-215
|
||||
void revisionTypeOfDefaultRevisionMetadataIsProperlySet() {
|
||||
|
||||
Object[] data = new Object[] { "a", mock(DefaultRevisionEntity.class), RevisionType.DEL };
|
||||
|
||||
EnversRevisionRepositoryImpl.QueryResult<Object> result = new EnversRevisionRepositoryImpl.QueryResult<>(data);
|
||||
|
||||
RevisionMetadata<?> revisionMetadata = result.createRevisionMetadata();
|
||||
|
||||
assertThat(revisionMetadata).isInstanceOf(DefaultRevisionMetadata.class);
|
||||
assertThat(revisionMetadata.getRevisionType()).isEqualTo(RevisionMetadata.RevisionType.DELETE);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright 2018-2021 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
|
||||
*
|
||||
* https://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 static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.envers.Config;
|
||||
import org.springframework.data.envers.sample.Country;
|
||||
import org.springframework.data.envers.sample.CountryQueryDslRepository;
|
||||
import org.springframework.data.envers.sample.QCountry;
|
||||
import org.springframework.data.history.Revision;
|
||||
import org.springframework.data.history.Revisions;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
/**
|
||||
* Integration tests for repositories with Querydsl support. They make sure that methods provided by both
|
||||
* {@link org.springframework.data.repository.history.RevisionRepository} and {@link org.springframework.data.querydsl.QuerydslPredicateExecutor} are working.
|
||||
*
|
||||
* @author Dmytro Iaroslavskyi
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = Config.class)
|
||||
class QueryDslRepositoryIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
CountryQueryDslRepository countryRepository;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
countryRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWithQueryDsl() {
|
||||
|
||||
Country de = new Country();
|
||||
de.code = "de";
|
||||
de.name = "Deutschland";
|
||||
|
||||
countryRepository.save(de);
|
||||
|
||||
Country found = countryRepository.findOne(QCountry.country.name.eq("Deutschland")).get();
|
||||
|
||||
assertThat(found).isNotNull();
|
||||
assertThat(found.id).isEqualTo(de.id);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWithRevisions() {
|
||||
|
||||
Country de = new Country();
|
||||
de.code = "de";
|
||||
de.name = "Deutschland";
|
||||
|
||||
countryRepository.save(de);
|
||||
|
||||
de.name = "Germany";
|
||||
|
||||
countryRepository.save(de);
|
||||
|
||||
Revisions<Integer, Country> revisions = countryRepository.findRevisions(de.id);
|
||||
|
||||
assertThat(revisions).hasSize(2);
|
||||
|
||||
Iterator<Revision<Integer, Country>> iterator = revisions.iterator();
|
||||
|
||||
Integer firstRevisionNumber = iterator.next().getRevisionNumber().get();
|
||||
Integer secondRevisionNumber = iterator.next().getRevisionNumber().get();
|
||||
|
||||
assertThat(countryRepository.findRevision(de.id, firstRevisionNumber).get().getEntity().name)
|
||||
.isEqualTo("Deutschland");
|
||||
assertThat(countryRepository.findRevision(de.id, secondRevisionNumber).get().getEntity().name).isEqualTo("Germany");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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
|
||||
*
|
||||
* https://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 static org.assertj.core.api.Assertions.*;
|
||||
import static org.springframework.data.history.RevisionMetadata.RevisionType.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.envers.Config;
|
||||
import org.springframework.data.envers.sample.Country;
|
||||
import org.springframework.data.envers.sample.CountryRepository;
|
||||
import org.springframework.data.envers.sample.License;
|
||||
import org.springframework.data.envers.sample.LicenseRepository;
|
||||
import org.springframework.data.history.Revision;
|
||||
import org.springframework.data.history.RevisionSort;
|
||||
import org.springframework.data.history.Revisions;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
/**
|
||||
* Integration tests for repositories.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = Config.class)
|
||||
class RepositoryIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
LicenseRepository licenseRepository;
|
||||
@Autowired
|
||||
CountryRepository countryRepository;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
|
||||
licenseRepository.deleteAll();
|
||||
countryRepository.deleteAll();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
|
||||
licenseRepository.deleteAll();
|
||||
countryRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLifeCycle() {
|
||||
|
||||
License license = new License();
|
||||
license.name = "Schnitzel";
|
||||
|
||||
licenseRepository.save(license);
|
||||
|
||||
Country de = new Country();
|
||||
de.code = "de";
|
||||
de.name = "Deutschland";
|
||||
|
||||
countryRepository.save(de);
|
||||
|
||||
Country se = new Country();
|
||||
se.code = "se";
|
||||
se.name = "Schweden";
|
||||
|
||||
countryRepository.save(se);
|
||||
|
||||
license.laender = new HashSet<>();
|
||||
license.laender.addAll(Arrays.asList(de, se));
|
||||
|
||||
licenseRepository.save(license);
|
||||
|
||||
de.name = "Daenemark";
|
||||
|
||||
countryRepository.save(de);
|
||||
|
||||
Optional<Revision<Integer, License>> revision = licenseRepository.findLastChangeRevision(license.id);
|
||||
|
||||
assertThat(revision).hasValueSatisfying(it -> {
|
||||
|
||||
Page<Revision<Integer, License>> page = licenseRepository.findRevisions(license.id, PageRequest.of(0, 10));
|
||||
Revisions<Integer, License> revisions = Revisions.of(page.getContent());
|
||||
assertThat(revisions.getLatestRevision()).isEqualTo(it);
|
||||
});
|
||||
}
|
||||
|
||||
@Test // #1
|
||||
void returnsEmptyLastRevisionForUnrevisionedEntity() {
|
||||
assertThat(countryRepository.findLastChangeRevision(100L)).isEmpty();
|
||||
}
|
||||
|
||||
@Test // #47
|
||||
void returnsEmptyRevisionsForUnrevisionedEntity() {
|
||||
assertThat(countryRepository.findRevisions(100L)).isEmpty();
|
||||
}
|
||||
|
||||
@Test // #47
|
||||
void returnsEmptyRevisionForUnrevisionedEntity() {
|
||||
assertThat(countryRepository.findRevision(100L, 23)).isEmpty();
|
||||
}
|
||||
|
||||
@Test // #31
|
||||
void returnsParticularRevisionForAnEntity() {
|
||||
|
||||
Country de = new Country();
|
||||
de.code = "de";
|
||||
de.name = "Deutschland";
|
||||
|
||||
countryRepository.save(de);
|
||||
|
||||
de.name = "Germany";
|
||||
|
||||
countryRepository.save(de);
|
||||
|
||||
Revisions<Integer, Country> revisions = countryRepository.findRevisions(de.id);
|
||||
|
||||
assertThat(revisions).hasSize(2);
|
||||
|
||||
Iterator<Revision<Integer, Country>> iterator = revisions.iterator();
|
||||
Revision<Integer, Country> first = iterator.next();
|
||||
Revision<Integer, Country> second = iterator.next();
|
||||
|
||||
assertThat(countryRepository.findRevision(de.id, first.getRequiredRevisionNumber()))
|
||||
.hasValueSatisfying(it -> assertThat(it.getEntity().name).isEqualTo("Deutschland"));
|
||||
|
||||
assertThat(countryRepository.findRevision(de.id, second.getRequiredRevisionNumber()))
|
||||
.hasValueSatisfying(it -> assertThat(it.getEntity().name).isEqualTo("Germany"));
|
||||
}
|
||||
|
||||
@Test // #55
|
||||
void considersRevisionNumberSortOrder() {
|
||||
|
||||
Country de = new Country();
|
||||
de.code = "de";
|
||||
de.name = "Deutschland";
|
||||
|
||||
countryRepository.save(de);
|
||||
|
||||
de.name = "Germany";
|
||||
|
||||
countryRepository.save(de);
|
||||
|
||||
Page<Revision<Integer, Country>> page = countryRepository.findRevisions(de.id,
|
||||
PageRequest.of(0, 10, RevisionSort.desc()));
|
||||
|
||||
assertThat(page).hasSize(2);
|
||||
assertThat(page.getContent().get(0).getRequiredRevisionNumber())
|
||||
.isGreaterThan(page.getContent().get(1).getRequiredRevisionNumber());
|
||||
}
|
||||
|
||||
@Test // #21
|
||||
void findsDeletedRevisions() {
|
||||
|
||||
Country de = new Country();
|
||||
de.code = "de";
|
||||
de.name = "Deutschland";
|
||||
|
||||
countryRepository.save(de);
|
||||
|
||||
countryRepository.delete(de);
|
||||
|
||||
Revisions<Integer, Country> revisions = countryRepository.findRevisions(de.id);
|
||||
|
||||
assertThat(revisions).hasSize(2);
|
||||
assertThat(revisions.getLatestRevision().getEntity()) //
|
||||
.isNotNull() //
|
||||
.extracting(c -> c.name, c -> c.code) //
|
||||
.containsExactly(null, null);
|
||||
}
|
||||
|
||||
@Test // #47
|
||||
void includesCorrectRevisionType() {
|
||||
|
||||
Country de = new Country();
|
||||
de.code = "de";
|
||||
de.name = "Deutschland";
|
||||
|
||||
countryRepository.save(de);
|
||||
|
||||
de.name = "Bundes Republik Deutschland";
|
||||
|
||||
countryRepository.save(de);
|
||||
|
||||
countryRepository.delete(de);
|
||||
|
||||
Revisions<Integer, Country> revisions = countryRepository.findRevisions(de.id);
|
||||
|
||||
assertThat(revisions) //
|
||||
.extracting(r -> r.getMetadata().getRevisionType()) //
|
||||
.containsExactly( //
|
||||
INSERT, //
|
||||
UPDATE, //
|
||||
DELETE //
|
||||
);
|
||||
}
|
||||
|
||||
@Test // #146
|
||||
void shortCircuitingWhenOffsetIsToLarge() {
|
||||
|
||||
Country de = new Country();
|
||||
de.code = "de";
|
||||
de.name = "Deutschland";
|
||||
|
||||
countryRepository.save(de);
|
||||
|
||||
countryRepository.delete(de);
|
||||
|
||||
check(de.id, 0, 1, 2);
|
||||
check(de.id, 1, 1, 2);
|
||||
check(de.id, 2, 0, 2);
|
||||
}
|
||||
|
||||
@Test // #47
|
||||
void paginationWithEmptyResult() {
|
||||
|
||||
check(23L, 0, 0, 0);
|
||||
}
|
||||
|
||||
void check(Long id, int page, int expectedSize, int expectedTotalSize) {
|
||||
|
||||
Page<Revision<Integer, Country>> revisions = countryRepository.findRevisions(id, PageRequest.of(page, 1));
|
||||
assertThat(revisions).hasSize(expectedSize);
|
||||
assertThat(revisions.getTotalElements()).isEqualTo(expectedTotalSize);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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
|
||||
*
|
||||
* https://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.sample;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
|
||||
@MappedSuperclass
|
||||
@EqualsAndHashCode
|
||||
abstract class AbstractEntity {
|
||||
|
||||
public @Id @GeneratedValue Long id;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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
|
||||
*
|
||||
* https://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.sample;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
import lombok.ToString;
|
||||
import org.hibernate.envers.Audited;
|
||||
|
||||
/**
|
||||
* Sample domain class.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@Audited
|
||||
@Entity
|
||||
@ToString
|
||||
public class Country extends AbstractEntity {
|
||||
|
||||
public String code;
|
||||
public String name;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2015-2021 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
|
||||
*
|
||||
* https://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.sample;
|
||||
|
||||
import org.springframework.data.envers.repository.support.EnversRevisionRepository;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
|
||||
|
||||
/**
|
||||
* Repository with QueryDsl support for {@link Country} objects.
|
||||
*
|
||||
* @author Dmytro Iaroslavskyi
|
||||
*/
|
||||
public interface CountryQueryDslRepository
|
||||
extends EnversRevisionRepository<Country, Long, Integer>, JpaRepository<Country, Long>,
|
||||
QuerydslPredicateExecutor<Country> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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
|
||||
*
|
||||
* https://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.sample;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.repository.history.RevisionRepository;
|
||||
|
||||
/**
|
||||
* Repository for {@link Country} objects.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface CountryRepository extends RevisionRepository<Country, Long, Integer>, JpaRepository<Country, Long> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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
|
||||
*
|
||||
* https://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.sample;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.ManyToMany;
|
||||
import jakarta.persistence.Version;
|
||||
|
||||
import org.hibernate.envers.Audited;
|
||||
|
||||
/**
|
||||
* Sample domain class.
|
||||
*
|
||||
* @author Philip Huegelmeyer
|
||||
*/
|
||||
@Audited
|
||||
@Entity
|
||||
public class License extends AbstractEntity {
|
||||
|
||||
@Version
|
||||
public Integer version;
|
||||
|
||||
public String name;
|
||||
@ManyToMany
|
||||
public Set<Country> laender;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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
|
||||
*
|
||||
* https://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.sample;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.repository.history.RevisionRepository;
|
||||
|
||||
/**
|
||||
* Repository for {@link License} objects.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface LicenseRepository extends RevisionRepository<License, Long, Integer>, JpaRepository<License, Long> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2015-2021 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
|
||||
*
|
||||
* https://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.sample;
|
||||
|
||||
import com.querydsl.core.types.Path;
|
||||
import com.querydsl.core.types.PathMetadata;
|
||||
import com.querydsl.core.types.dsl.EntityPathBase;
|
||||
import com.querydsl.core.types.dsl.PathInits;
|
||||
import com.querydsl.core.types.dsl.StringPath;
|
||||
|
||||
import static com.querydsl.core.types.PathMetadataFactory.forVariable;
|
||||
|
||||
/**
|
||||
* Query class for Country domain.
|
||||
*
|
||||
* @author Dmytro Iaroslavskyi
|
||||
*/
|
||||
public class QCountry extends EntityPathBase<Country> {
|
||||
|
||||
private static final long serialVersionUID = -936338527;
|
||||
|
||||
private static final PathInits INITS = PathInits.DIRECT2;
|
||||
|
||||
public static final QCountry country = new QCountry("country");
|
||||
|
||||
public final StringPath code = createString("code");
|
||||
public final StringPath name = createString("name");
|
||||
|
||||
public QCountry(String variable) {
|
||||
this(Country.class, forVariable(variable), INITS);
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public QCountry(Path<? extends Country> path) {
|
||||
this((Class) path.getType(), path.getMetadata(), path.getMetadata().isRoot() ? INITS : PathInits.DEFAULT);
|
||||
}
|
||||
|
||||
public QCountry(PathMetadata metadata) {
|
||||
this(metadata, metadata.isRoot() ? INITS : PathInits.DEFAULT);
|
||||
}
|
||||
|
||||
public QCountry(PathMetadata metadata, PathInits inits) {
|
||||
this(Country.class, metadata, inits);
|
||||
}
|
||||
|
||||
public QCountry(Class<? extends Country> type, PathMetadata metadata, PathInits inits) {
|
||||
super(type, metadata, inits);
|
||||
}
|
||||
|
||||
}
|
||||
16
spring-data-envers/src/test/resources/logback.xml
Normal file
16
spring-data-envers/src/test/resources/logback.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework.data" level="warn" />
|
||||
|
||||
<root level="warn">
|
||||
<appender-ref ref="console" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user