我必须重新实现几个后端服务,其中一个主要的要求是使整个流程反应。以前,PostgreSQL使用hibernate服务,因此上述连接是由框架提供的。
由于我必须保留原始DB,只需更改服务实现,所以我必须使用r2dbc-postgresql。我找不到关于这个主题的任何资源,但我最好的猜测是做一些类似于使用JDBC的操作,并在实体之间引入一些新的连接表。
发布于 2020-02-16 14:42:50
我正在调查类似的事情,并得出了同样的结论(1),因为在R2DBC中没有人支持这种关系。为了迁移一对多的关系,我首先将包含“多”实体的集合转换为“一个”实体中的“瞬态”实体。持久化“一个”实体是使用以下步骤按反应顺序完成的:
在代码中,如下所示:
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);
});
});
}到目前为止,我的结论是,除非您确信切换到R2DBC会带来很大的好处,否则我可以使用Spring并使用subscribeOn在一个单独的线程中执行对存储库的调用。
祝你好运,编码愉快!
https://stackoverflow.com/questions/59986014
复制相似问题