如何在以前使用 JPA 的项目中使用 R2DBC 实现 OneToMany、ManyToOne 和 ManyToMany?

gab*_*tal 17 postgresql project-reactor spring-webflux spring-data-r2dbc r2dbc

我必须重新实现一些后端服务,主要要求之一是使整个流程具有反应性。以前,服务使用 PostgreSQL 休眠,因此上述连接是由框架提供的。

由于我必须保留原始数据库并更改服务实现,因此我必须使用 r2dbc-postgresql。我找不到关于这个主题的任何资源,但我最好的猜测是做一些类似于我会用 JDBC 做的事情,并在我的实体之间引入一些新的连接表。

  1. 这是一种正确的方法还是我应该考虑一些不同的解决方案?
  2. 实现上述连接的步骤是什么?

Iva*_*san 12

我正在研究类似的东西并得出了相同的结论 (1),因为在 R2DBC 中不支持关系。为了迁移一对多关系,我首先将包含“多个”实体的集合创建到“一个”实体中的 @Transient。持久化“一个”实体是使用以下反应序列中的步骤完成的:

  1. 坚持“许多”实体。这是为了能够在“一个”实体中更新这些,以便为“多个”实体分配 id。
  2. 坚持“一个”实体。
  3. 保持关系。我可以在这个阶段做到这一点,因为我现在拥有所有相关实体的 ID。对于关系,我引入了一个辅助实体,类似于 OneManyRelation 和相应的存储库。

在代码中它看起来像这样:

public <S extends Drawing> Mono<S> save(final S inDrawing) {
    final List<Shape> theDrawingShapes = inDrawing.getShapes();

    return Mono.defer(() -> {
        return Flux.fromIterable(theDrawingShapes)
            .log()
            .flatMap(theDrawingShape -> {
                /* Save the shapes contained in the drawing. */
                if (theDrawingShape instanceof Circle) {
                    final Circle theUnsavedCircle = (Circle) theDrawingShape;
                    return mCircleRepository.save(theUnsavedCircle);
                } else if (theDrawingShape instanceof Rectangle) {
                    final Rectangle theUnsavedRectangle = (Rectangle) theDrawingShape;
                    return mRectangleRepository.save(theUnsavedRectangle);
                } else {
                    LOGGER.warn("Unrecognized entity type: {}",
                        theDrawingShape.getClass().getName());
                    return Mono.just(theDrawingShape);
                }
            })
            /* Update the drawing, setting the shapes of the drawing to the saved shapes. */
            .collectList()
            .map(theSavedShapesList -> {
                inDrawing.setShapes(new ArrayList<>(theSavedShapesList));
                return inDrawing;
            })
            /* Save the drawing itself. */
            .flatMap(theDrawing -> super.save(theDrawing))
            .flatMap(theDrawing -> {
                /* Save the relations between the drawing and the shapes of the drawing. */
                return Flux.fromIterable(theDrawing.getShapes())
                    .flatMap(theDrawingShape -> {
                        final var theDrawingShapeRelation = new DrawingShapesRelation();
                        theDrawingShapeRelation.setDrawingId(theDrawing.getId());
                        theDrawingShapeRelation.setShapeId(theDrawingShape.getId());
                        theDrawingShapeRelation.setShapeType(theDrawingShape.getClass()
                            .getName());
                        return mDrawingShapesRelationRepository.save(theDrawingShapeRelation);
                    })
                    .collectList()
                    .map(theDrawingShapesRelationList -> theDrawing);
            });
    });
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我的结论是,除非您确定切换到 R2DBC 会带来重大收益,否则我会满足于使用 Spring Data JPA 并使用 subscribeOn 在单独的线程中执行对存储库的调用。
祝你好运,编码愉快!