Spring Data: Override save method

前端 未结 11 1286
温柔的废话
温柔的废话 2020-11-27 05:30

I\'m considering spring data for a project. Is it possible to override the per default generated save method? And if yes, how?

11条回答
  •  伪装坚强ぢ
    2020-11-27 05:44

    What works for me (Spring boot 2.x Java 11), even if not perfectly clean. It compiles with IDE and Maven and Gradle. The above solution by Lucas does not work for me for the JpaRepository.

    public interface MyRepo extends JpaRepository, MyRepoCustom{
    
       //Implemented in MyRepoCustom
       public MyType save(MyType mytypeEntity);
    }
    

    The custom interface (repeats the declaration, which is not nice):

    public interface MyRepoCustom{
        public MyType save(MyType mytypeEntity);
    }
    

    The custom Impl:

    @Repository
    public class MyRepoImpl implements MyRepoCustom{
        @PersistenceContext
        private EntityManager em;
    
        @Transactional
        public MyType save(MyType mytypeEntity) {
           //type safe implementation
        }
    }
    

提交回复
热议问题