Spring Data:覆盖保存方法

我正在考虑项目的弹簧数据。 是否可以覆盖每个默认生成的保存方法? 如果有,怎么样?

只需像往常一样创建自定义界面,并使用与CrudRepository (或JpaRepository等)公开的签名相同的签名声明您想要ovverride的方法。 假设您有一个MyEntity实体和一个MyEntityRepository存储库,并且您希望覆盖MyEntityRepository的默认自动生成的save方法,该方法只接受一个实体实例,然后定义:

 public interface MyEntityRepositoryCustom {  S save(S entity); } 

并像往常一样在MyEntityRepositoryImpl实现此方法:

 @Transactional public class MyEntityRepositoryImpl implements MyEntityRepositoryCustom { public  S save(S entity) { // your implementation } } 

然后,像往常一样,让MyEntityRepository实现MyEntityRepositoryCustom

这样做,Spring Data JPA将调用MyEntityRepositoryImplsave方法而不是默认实现。 至少这对我来说适用于Spring Data JPA 1.7.2中的delete方法。

我想你扩展SimpleJpaRepository:

 public class **CustomSimpleJpaRepository** extends SimpleJpaRepository { @Transactional public  S save(S entity) { //do what you want instead } } 

然后通过扩展来确保使用它而不是默认的SimpleJpaRepository:

 public class CustomJpaRepositoryFactory extends JpaRepositoryFactory { protected  JpaRepository getTargetRepository(RepositoryMetadata metadata, EntityManager entityManager) { Class repositoryInterface = metadata.getRepositoryInterface(); JpaEntityInformation 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 , S, ID extends Serializable> extends JpaRepositoryFactoryBean { protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) { return new **CustomJpaRepositoryFactory**(entityManager); } 

}

配置:

  

希望能帮助到你。

要提供对默认生成的save方法的覆盖,您需要在自己的自定义存储库实现中使用Spring Data存储库实现的聚合。

存储库界面:

 public interface UserRepository extends CrudRepository{ } 

您的存储库实现:

 @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; 

没有得到这个很好地工作所以我把我需要的逻辑放入一个服务类,并保持存储库保存方法不变。

使用JPA事件监听器,如@PrePersist,@ PreUpdate。 如果基础JPA提供程序支持此function,这将起作用。 这是JPA 2的function,所以最新的Hibernate,EclipseLink等应该支持它。

如果您要重用原始方法,这可能会有所帮助。 只需在实现类中注入EntityManager

 public interface MyEntityRepositoryCustom {  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 save(S entity) { // do your logic here JpaEntityInformation entityInformation = JpaEntityInformationSupport.getMetadata(MyEntity.class, entityManager); if (entityInformation.isNew(entity)) { entityManager.persist(entity); return entity; } else { return entityManager.merge(entity); } } }