Spring Data предоставляет EnversRevisionRepositoryImpl
по умолчанию RevisionRepository
реализации.
Как вы можете видеть в конструкторе репозитория:
/**
* 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;
}
вам нужны три вещи, чтобы создать новый экземпляр этого репозитория:
EntityManager
, который можно легко получить с помощью @PersistenceContext
или, возможно (я никогда не проверял) с помощью @Autowiring
.
А JpaEntityInformation
. Следуя примеру SimpleJpaRepository
:
final JpaEntityInformation<T, ?> entityInformation =
JpaEntityInformationSupport.getEntityInformation(domainClass, em)
- А
RevisionEntityInformation
. Любопытно, поскольку утверждается, что объект не является нулевым, но на самом деле он кажется неиспользуемым в остальной части кода реализации. Здесь мы можем последовать примеру EnversRevisionRepositoryFactoryBean
:
final RevisionEntityInformation revisionEntityInformation =
Optional.ofNullable(revisionEntityClass) //
.filter(it -> !it.equals(DefaultRevisionEntity.class))//
.<RevisionEntityInformation> map(ReflectionRevisionEntityInformation::new) //
.orElseGet(DefaultRevisionEntityInformation::new);
DefaultRevisionEntityInformation
находится в пределах пакета, но вы можете легко воспроизвести его:
import org.hibernate.envers.DefaultRevisionEntity;
import org.springframework.data.repository.history.support.RevisionEntityInformation;
/**
* {@link RevisionEntityInformation} for {@link DefaultRevisionEntity}.
*/
public 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;
}
}
В итоге:
// Obtain a reference to the underlying EntityManager
final EntityManager entityManager = ...;
// Provide JpaEntityInformation
final JpaEntityInformation<User, ?> entityInformation =
JpaEntityInformationSupport.getEntityInformation(User.class, entityManager);
// Provide RevisionEntityInformation. The solution is based on
// DefaultRevisionEntity but you can provide your custom revision entity
// if appropriate
final RevisionEntityInformation revisionEntityInformation =
new DefaultRevisionEntityInformation();
// Build your custom revision repository, for example:
final RevisionRepository userRevisionRepository =
new EnversRevisionRepositoryImpl<User, Integer, DefaultRevisionEntity>(
entityInformation, revisionEntityInformation, entityManager
)
;
person
jccampanero
schedule
16.03.2021