gpt4 book ai didi

database - ReactiveMongoRepository没有保存我的数据

转载 作者:行者123 更新时间:2023-12-02 12:15:16 24 4
gpt4 key购买 nike

我对springmongonoSQL不满意。

我已经在本地连接到我的mongoDB。

但是,我保存实体的尝试始终失败。

val a0 = myEntity(name = "my Entity")
repository.save(a0)

我的存储库扩展了 ReactiveMongoRepository<MyEntity, Long>

但是,当我更改它以扩展 MongoRepository<MyEntity, Long>时,它就可以工作。

可能是什么问题?

我的数据库实例正在运行并且运行良好。
我猜想它必须对 react 性事情有所作为。

最佳答案

如果您使用响应式(Reactive)流,则应注意nothing happens until you subscribe:

In Reactor, when you write a Publisher chain, data does not start pumping into it by default. Instead, you create an abstract description of your asynchronous process (which can help with reusability and composition).

By the act of subscribing, you tie the Publisher to a Subscriber, which triggers the flow of data in the whole chain. This is achieved internally by a single request signal from the Subscriber that is propagated upstream, all the way back to the source Publisher.


这意味着,如果您使用 ReactiveMongoRepository,则必须订阅响应流。这可以通过在任何 subscribe()上使用 Publisher方法来完成。例如,使用Java,将是:
reactiveRepository
.save(myEntity)
.subscribe(result => log.info("Entity has been saved: {}", result));
此外,如果您编写响应式(Reactive) Controller ,Webflux之类的框架将为您处理订阅。使用Java意味着您可以编写如下内容:
@GetMapping
public Mono<MyEntity> save() {
return reactiveRepository.save(myEntity); // subscribe() is done by the Webflux framework
}
显然,响应式编程不仅有很多其他功能,而且您应该意识到,如果不适应响应式生态系统和使用响应式编程,就不能简单地将 MongoRepository交换为 ReactiveMongoRepository
Reactor引用指南中有 Introduction to Reactive Programming一章,可能很有趣。之前引用的文档也来自引用指南的这一章。

关于database - ReactiveMongoRepository没有保存我的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57070561/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com