gab*_*tal 17 postgresql project-reactor spring-webflux spring-data-r2dbc r2dbc
我必须重新实现一些后端服务,主要要求之一是使整个流程具有反应性。以前,服务使用 PostgreSQL 休眠,因此上述连接是由框架提供的。
由于我必须保留原始数据库并更改服务实现,因此我必须使用 r2dbc-postgresql。我找不到关于这个主题的任何资源,但我最好的猜测是做一些类似于我会用 JDBC 做的事情,并在我的实体之间引入一些新的连接表。
Iva*_*san 12
我正在研究类似的东西并得出了相同的结论 (1),因为在 R2DBC 中不支持关系。为了迁移一对多关系,我首先将包含“多个”实体的集合创建到“一个”实体中的 @Transient。持久化“一个”实体是使用以下反应序列中的步骤完成的:
在代码中它看起来像这样:
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 在单独的线程中执行对存储库的调用。
祝你好运,编码愉快!
| 归档时间: |
|
| 查看次数: |
4538 次 |
| 最近记录: |