How to retrieve spring data repository instance for given domain class?

后端 未结 3 1928
夕颜
夕颜 2020-12-24 07:47

Given the list of all spring data repositories in some class Bar:

@Autowired
private List repositories;

How

3条回答
  •  隐瞒了意图╮
    2020-12-24 08:35

    @Service
    public class GenericRepository {
    
        @Autowired
        private WebApplicationContext appContext;
    
        Repositories repositories = null;
    
        public GenericRepository() {
            repositories = new Repositories(appContext);
        }
    
        public JpaRepository getRepository(AbstractPersistable entity) {
            return (JpaRepository) repositories.getRepositoryFor(entity.getClass());
        }
    
        public Object save(AbstractPersistable entity) {
            return getRepository(entity).save(entity);
        }
    
        public Object findAll(AbstractPersistable entity) {
            return getRepository(entity).findAll();
        }
    
        public void delete(AbstractPersistable entity) {
            getRepository(entity).delete(entity);
        }
    }
    

提交回复
热议问题