英文:
Disable Write operation in ReactiveCrudRepository
问题
在我的情况下,由于某种原因,我需要从不同原因拥有的表中读取数据。在我的应用程序中,我希望禁用对表的写入。我认为的一种解决方法是继承ReactiveCrudRepository
并创建一个在写入/更新操作上引发异常的接口。
是否有其他干净和可维护的方法可用于这种用例?
英文:
In my case, due to some reason, I need to read data from the tables owned by different reason. In my application, I want to disable write on the table. One solution I think is to inherit ReactiveCrudRepository
and create an interface which throws exception on write/update operation.
Is there any other clean and maintainable method available for such use-cases?
答案1
得分: 1
第一选项
创建一个只读用户(具有仅读取权限)在数据库中,并使用此用户从您的应用程序连接到数据库。
第二选项
如果第一选项在您的情况下不合适,则:
创建一个通用存储库,其中只包含用于读取的方法,如下所示:(从Repository
扩展,因为它不包含任何方法)
@NoRepositoryBean
public interface ReadOnlyReactiveRepository<T, ID> extends Repository<T, ID> {
Flux<T> findAll();
Mono<T> findById(ID id);
... 你想要的任何方法
}
注意: 不要忘记用@NoRepositoryBean
注释此通用存储库,该注释用于避免为实际上符合存储库接口的标准但不打算成为存储库的接口创建存储库代理。
然后从该存储库扩展您的域存储库,而不是从ReactiveCrudRepository
扩展,如下所示:
public interface YourDomainRepository extends ReadOnlyReactiveRepository<YourDomain, String> {
}
英文:
I see two options here:
First option
Create a read-only user (which has permissions only for reading) in DB and connect to DB from your application with this user.
Second option
If the first option is not suitable in your case, then:
Create a generic repository which has only methods for reading like this: (extend it from Repository
as it does not contain any method)
@NoRepositoryBean
public interface ReadOnlyReactiveRepository<T, ID> extends Repository<T, ID> {
Flux<T> findAll();
Mono<T> findById(ID id);
... any methods you want
}
Note: Dont forget to annotate this generic repository with @NoRepositoryBean
which is used to avoid creating repository proxies for interfaces that actually match the criteria of a repo interface but are not intended to be one.
And extend your domain repository from that repository, not from ReactiveCrudRepository
like this:
public interface YourDomainRepository extends ReadOnlyReactiveRepository<YourDomain, String> {
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论