Enforce a criteria on all Spring Repository find*

2019-02-20 05:59发布

Is there a way to apply a default filter/predicate on all queries that are generated by a JpaRepository, to automatically restrict the results. For example, if I implement a soft-delete flag, I want to automatically exclude those results from all further selects.

Hibernate is the JPA implementation beneath, if that helps.

2条回答
时光不老,我们不散
2楼-- · 2019-02-20 06:25

You can create your own repository that extends SimpleJpaRepository. Then implement RepositoryFactoryBean and RepositoryFactory that will return repositories containing your implementation. For JPA you can base on the example under https://github.com/jkubrynski/spring-data-examples/tree/master/src/main/java/com/kubrynski/data/repository/generic

Remember about enabling your factorybean in @EnableJpaRepositories by specifying repositoryFactoryBeanClass.

查看更多
祖国的老花朵
3楼-- · 2019-02-20 06:40

See here where there is the answer to your question (basically using the @Where annotation). You could also use hibernate filters but the answer I linked above is more than enough.

Even if your question is old I write here for future reference.

查看更多
登录 后发表回答