如何创建EnversRevisionRepository
动态给定的 Java Class 对象?
感谢这个答案https://stackoverflow.com/a/22342025/3771679可以成功地创建一个JpaRepository
动态的,但我想实现相同的存储库,相当于:
public interface MyRevisionedRepository extends RevisionRepository<X,Y,Z>, JpaRepository<X,Y> {
}
如何创建EnversRevisionRepository
动态给定的 Java Class 对象?
感谢这个答案https://stackoverflow.com/a/22342025/3771679可以成功地创建一个JpaRepository
动态的,但我想实现相同的存储库,相当于:
public interface MyRevisionedRepository extends RevisionRepository<X,Y,Z>, JpaRepository<X,Y> {
}
Spring DataEnversRevisionRepositoryImpl
作为默认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;
}
您需要三件事来创建该存储库的新实例:
An 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
)
;