我正在考虑项目的弹簧数据。是否可以覆盖每个默认生成的保存方法?如果是,怎么样?
只需像往常一样创建自定义界面,并使用与CrudRepository
(或JpaRepository
等)公开的方法相同的签名声明您想要ovverride的方法。假设您有一个MyEntity
实体和一个MyEntityRepository
存储库,并且您想要覆盖save
的默认自动生成的MyEntityRepository
方法,该方法只接受一个实体实例,然后定义:
public interface MyEntityRepositoryCustom {
<S extends MyEntity> S save(S entity);
}
像往常一样在你的MyEntityRepositoryImpl
中实现这个方法:
@Transactional
public class MyEntityRepositoryImpl implements MyEntityRepositoryCustom {
public <S extends MyEntity> S save(S entity) {
// your implementation
}
}
然后,像往常一样,让MyEntityRepository
实施MyEntityRepositoryCustom
。
这样做,Spring Data JPA将调用save
的MyEntityRepositoryImpl
方法而不是默认实现。至少这对我来说适用于Spring Data JPA 1.7.2中的delete
方法。
为了正确覆盖save方法,您必须创建一个接口,其中包含在CrudRepository上声明的原始方法的正确签名,包括泛型
public interface MyCustomRepository<T> {
<S extends T> S save(S entity);
}
然后,创建您的实现(后缀Impl在类的名称中很重要)
public class MyCustomRepositoryImpl implements MyCustomRepository<MyBean> {
@Autowired
private EntityManager entityManager;
@Override
public <S extends MyBean> S save(S entity) {
/**
your custom implementation comes here ...
i think the default one is just
return this.entityManager.persist(entity);
*/
}
}
最后,使用先前创建的界面扩展您的存储库
@RepositoryRestResource
@Repository
public interface MyBeanRepository extends PagingAndSortingRepository<MyBean, Long>, MyCustomRepository<MyBean> {}
我想你扩展SimpleJpaRepository:
public class **CustomSimpleJpaRepository** extends SimpleJpaRepository {
@Transactional
public <S extends T> S save(S entity) { //do what you want instead }
}
然后通过扩展来确保使用它而不是默认的SimpleJpaRepository:
public class CustomJpaRepositoryFactory extends JpaRepositoryFactory {
protected <T, ID extends Serializable> JpaRepository<?, ?> getTargetRepository(RepositoryMetadata metadata, EntityManager entityManager) {
Class<?> repositoryInterface = metadata.getRepositoryInterface();
JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType());
SimpleJpaRepository<?, ?> repo = isQueryDslExecutor(repositoryInterface) ? new QueryDslJpaRepository(
entityInformation, entityManager) : new CustomSimpleJpaRepository(entityInformation, entityManager);
repo.setLockMetadataProvider(lockModePostProcessor.getLockMetadataProvider());
return repo;
}
}
还没有完成,我们还需要你自己的工厂bean在config xml中使用它:
public class CustomRepositoryFactoryBean <T extends JpaRepository<S, ID>, S, ID extends Serializable> extends JpaRepositoryFactoryBean<T, S, ID> {
protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
return new **CustomJpaRepositoryFactory**(entityManager);
}
}
配置:
<jpa:repositories base-package="bla.bla.dao" factory-class="xxxx.**CustomRepositoryFactoryBean**"/>
希望能帮助到你。
要提供对默认生成的save方法的覆盖,您需要在自己的自定义存储库实现中使用Spring Data存储库实现的聚合。
存储库界面:
public interface UserRepository extends CrudRepository<User, String>{
}
您的存储库实现:
@Repository("customUserRepository")
public class CustomUserRepository implements UserRepository {
@Autowired
@Qualifier("userRepository") // inject Spring implementation here
private UserRepository userRepository;
public User save(User user) {
User user = userRepository.save(entity);
// Your custom code goes here
return user;
}
// Delegate other methods here ...
@Override
public User findOne(String s) {
return userRepository.findOne(s);
}
}
然后在您的服务中使用您的自定义实现:
@Autowired
@Qualifier("customUserRepository")
private UserRepository userRepository;
没有得到这个很好地工作所以我把我需要的逻辑放入一个服务类,并保持存储库保存方法不变。
如果您只使用接口,则可以使用默认方法来执行CrudRepository
或JpaRepository
的简单覆盖:
public interface MyCustomRepository extends CrudRepository<T, ID> {
@Override
default <S extends T> S save(S entity)
{
throw new UnsupportedOperationException("writes not allowed");
}
}
如果您要重用原始方法,这可能会有所帮助。只需在实施类中注入EntityManager
。
public interface MyEntityRepositoryCustom {
<S extends MyEntity> S save(S entity);
}
public class MyEntityRepositoryImpl implements MyEntityRepositoryCustom {
// optionally specify unitName, if there are more than one
@PersistenceContext(unitName = PRIMARY_ENTITY_MANAGER_FACTORY)
private EntityManager entityManager;
/**
* @see org.springframework.data.jpa.repository.support.SimpleJpaRepository
*/
@Transactional
public <S extends MyEntity> S save(S entity) {
// do your logic here
JpaEntityInformation<MyEntity, ?> entityInformation = JpaEntityInformationSupport.getMetadata(MyEntity.class, entityManager);
if (entityInformation.isNew(entity)) {
entityManager.persist(entity);
return entity;
} else {
return entityManager.merge(entity);
}
}
}
使用JPA事件监听器,如@PrePersist,@ PreUpdate。如果基础JPA提供程序支持此功能,这将起作用。这是JPA 2的功能,所以最新的Hibernate,EclipseLink等应该支持它。
我在OpenJDK 11上使用Spring Boot 2.1.4并且还从编译器中获取ambiguous reference
错误(尽管我的IDE使用的Eclipse JDT编译器没有问题,所以在我尝试之前我没有发现这个问题在我的IDE之外构建它)。
我基本上最终在扩展接口中定义了一个具有不同名称的方法,然后在我的主存储库接口中使用default
覆盖,在调用正常的save()
时调用它。
这是一个例子:
像往常一样定义自定义逻辑的接口:
public interface MyEntityRepositoryCustomSaveAction {
public MyEntity saveSafely(MyEntity entity);
}
使您的存储库扩展该接口:
public interface MyEntityRepository extends JpaRepository<MyEntity, MyEntityId>,
MyEntityRepositoryCustomSaveAction {
@Override
@SuppressWarnings("unchecked")
default MyEntity save(MyEntity entity)
{
return saveSafely(entity);
}
}
请注意,我们已经从JpaRepository
(以及CrudRepository
扩展的JpaRepository
)覆盖了save()来调用我们的自定义方法。编译器警告未经检查的转换,如果你想用@SuppressWarnings
使它静音,那么由你来决定。
使用自定义逻辑遵循Impl类的约定
public class MyEntityRepositoryCustomSaveActionImpl implements
MyEntityRepositoryCustomSaveAction {
@PersistenceContext
private EntityManager entityManager;
@Override
public MyEntity saveSafely(MyEntity entity) {
//whatever custom logic you need
}
}
来自@ytterrr的解决方案适用于旧的Spring Data版本,至少对于Spring Data 2.1,这不仅可以覆盖任何存储库方法,还可以访问底层功能(访问实体管理器以保持,删除,查找...):
public interface MyEntityRepositoryCustom {
<S extends MyEntity> S save(S entity);
}
public class MyEntityRepositoryImpl implements MyEntityRepositoryCustom {
final JpaEntityInformation<MyEntity, ?> entityInformation;
EntityManager em;
public MyEntityRepositoryImpl(EntityManager entityManager) {
this.entityInformation = JpaEntityInformationSupport.getEntityInformation(MyEntity.class, entityManager);
this.em = entityManager;
}
/**
* @see org.springframework.data.jpa.repository.support.SimpleJpaRepository
*/
@Transactional
public <S extends MyEntity> S save(S entity) {
// do your logic here
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
}