如何将数据库引用与反应式 Spring Data MongoDB 一起使用? [英] How to use db references with reactive Spring Data MongoDB?

查看:46
本文介绍了如何将数据库引用与反应式 Spring Data MongoDB 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 MongoDB 和 Reactor 的新手,我正在尝试检索一个用户及其关联的配置文件这是 POJO:

I'm new to MongoDB and Reactor and I'm trying to retrieve a User with its Profiles associated Here's the POJO :

public class User {

    private @Id String id;
    private String login;
    private String hashPassword;
    @Field("profiles") private List<String> profileObjectIds;
    @Transient private List<Profile> profiles; }

public class Profile {

    private @Id String id;
    private @Indexed(unique = true) String name;
    private List<String> roles; }

问题是,如何在 User POJO 中注入配置文件?

The problem is, how do I inject the profiles in the User POJO ?

我知道我可以放置 @DBRef 并解决问题,但在它的文档中,MongoDB 指定手动 Ref 应该优先于 DB ref.

I'm aware I can put a @DBRef and solve the problem but in it's documentation, MongoDB specify manual Ref should be preferred over DB ref.

我看到了两种解决方案:

I'm seeing two solutions :

  1. 当我拿到 pojo 时填写 :

  1. Fill the pojo when I get it :

public Mono<User> getUser(String login) {
    return userRepository.findByLogin(login)
    .flatMap(user -> ??? );
}

我应该对 profileRepository.findAllById() 做一些事情,但我不知道或连接两个发布者,因为配置文件结果取决于用户结果.

I should do something with profileRepository.findAllById() but I don't know or to concatene both Publishers given that profiles result depends on user result.

  1. 声明一个 AbstractMongoEventListener 并覆盖 onAfterConvert 方法:

但是这里我错了,因为方法在结果发布之前就结束了

But here I am mistaken since the method end before the result is Published

public void onAfterConvert(AfterConvertEvent<User> event) {
    final User source = event.getSource();
    source.setProfiles(new ArrayList<>());
    profileRepository.findAllById(source.getProfileObjectIds())
    .doOnNext(e -> source.getProfiles().add(e))
    subscribe();
}

推荐答案

TL;DR

响应式 Spring Data MongoDB 中没有 DBRef 支持,我不确定会有.

TL;DR

There's no DBRef support in reactive Spring Data MongoDB and I'm not sure there will be.

Spring Data 项目被组织成模板 API、转换器和映射元数据组件.模板 API 的命令式(阻塞)实现使用命令式方法来获取 Document 并将它们转换为域对象.MappingMongoConverter 特别处理所有的转换和 DBRef 解析.此 API 在同步/命令式 API 中工作,可用于模板 API 实现(命令式和响应式).

Spring Data projects are organized into Template API, Converter and Mapping Metadata components. The imperative (blocking) implementation of the Template API uses an imperative approach to fetch Documents and convert these into domain objects. MappingMongoConverter in particular handles all the conversion and DBRef resolution. This API works in a synchronous/imperative API and is used for both Template API implementations (imperative and the reactive one).

重用 MappingMongoConverter 是添加响应式支持的合乎逻辑的决定,因为我们不需要重复代码.唯一的限制是 DBRef 解析不适​​合反应式执行模型.

Reuse of MappingMongoConverter was the logical decision while adding reactive support as we don't have a need to duplicate code. The only limitation is DBRef resolution that does not fit the reactive execution model.

为了支持反应式DBRefs,转换器需要分成几个位,整个关联处理需要大修.

To support reactive DBRefs, the converter needs to be split up into several bits and the whole association handling requires an overhaul.

参考:https://jira.spring.io/browse/DATAMONGO-2146

将引用保留为域模型中的键/ID,并根据需要查找这些.zipWithflatMap 是合适的运算符,具体取决于您要存档的内容(使用引用增强模型,仅查找引用).

Keep references as keys/Id's in your domain model and look up these as needed. zipWith and flatMap are the appropriate operators, depending on what you want to archive (enhance model with references, lookup references only).

在相关说明中:Reactive Spring Data MongoDB 部分带有减少的功能集.上下文 SpEL 扩展是一项不受支持的功能,因为这些组件采用命令式编程模型,因此采用同步执行.

On a related note: Reactive Spring Data MongoDB comes partially with a reduced feature set. Contextual SpEL extension is a feature that is not supported as these components assume an imperative programming model and thus synchronous execution.

这篇关于如何将数据库引用与反应式 Spring Data MongoDB 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆