我不知道如何使用 spring-webflux (反应式)在 R2dbc (java)中构造有效的查询。使用 R2dbc 提供的 DatabaseClient 对象(或者 Connection 对象),我似乎只能调用这两种方法之一的不同变体:bind(Object field, Object value)或bindNull(Object field, Class<?> type)。如果我有一个模式,以及 Java 中的相应类,具有多个可为 null 的字段,那么我应该如何[某种程度上]有效地处理这个问题?
举个例子:
public Flux<Item> saveOrUpdate(Item entity) {
Mono<Connection> connection = this.connection;
Flux<? extends Result> itemFlux = connection
.doOnError(e -> e.printStackTrace())
.flatMapMany(connect -> connect.createStatement(INSERT_OR_UPDATE_ITEM)
.bind("itemId", entity.getItemId()).returnGeneratedValues("itemid")
.bind("auditId", entity.getTx().getId())
.bind("itemNum", entity.getItemNum())
.bind("itemCat", entity.getItemCat()) //nullable
// How would I know when to use this?
.bindNull("sourcedQty", Integer.class) //nullable
.bind("makeQty", entity.getMakeQty())
.bind("nameShown", entity.getNameShown()) //nullable
.bind("price", entity.price())
.bind("dateCreated", entity.getDateCreated()) //nullable
.add()
.execute())... …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试在 Spring Boot 中为已经处于开发阶段的应用程序实现 R2DBC - 不幸的是,这意味着我们的 DDL 不灵活,因为其他微服务依赖于此,反之亦然。R2DBC 的一个重要限制是不支持嵌套类。此外,它不支持 JPA,因为这会破坏全面非阻塞的目的。因此,所有这些有用的注释也不在表中。
我正在处理的类看起来像这样:
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Stats {
@Id
StatsPK statsPK
@Column(stat_name)
String stat;
...
@Data
@AllArgsConstructor
@NoArgsConstructor
@Embeddable
public static class StatsPK implements Serializable {
@Column("store_id")
private Integer storeId;
@Column("empl_id")
private String emplId;
@Column("app_version")
private String appVersion;
}
}
Run Code Online (Sandbox Code Playgroud)
在研究了我的问题后,我找到了很多方法来计算复合键,包括上面示例中已经显示的内容(使用 @Embeddable 注释),使用自定义 PK 对象(类似,但会有使用为此目的设计的类的选项,例如 CompositeKeyHolder)、JPA @TableId(TableId.class) 注释,以及其他一些似乎不太有希望的注释。
所有这些选项似乎都在使用 JPA 或使用嵌套对象,这是我做不到的。我真的想不出绕过这些限制的方法。但由于我是初学者,我决定询问是否有人以前处理过这个问题。感谢任何反馈。