1

尝试扩展 Spring BootReactiveCrudRepository接口,使其具有单独的实体插入和更新方法。现在save()方法区分插入和更新检查 ID 是否提供。为什么我需要以这种方式扩展它的想法是因为即将到来的带有新实体或修改实体的 kafka 事件已经填充了 ID。

CustomReactiveCrudRepository:

public interface CustomReactiveCrudRepository<T, ID> extends ReactiveCrudRepository<T, ID> {

    <S extends T> Mono<S> insert(S entity);

    <S extends T> Mono<S> update(S entity);

}

CustomReactiveCrudRepositoryImpl:

public class CustomReactiveCrudRepositoryImpl<T, ID> extends SimpleR2dbcRepository<T, ID> implements CustomReactiveCrudRepository<T, ID> {

    private final RelationalEntityInformation<T, ID> entity;
    private final DatabaseClient                     databaseClient;

    public CustomReactiveCrudRepositoryImpl(RelationalEntityInformation<T, ID> entity, DatabaseClient databaseClient, R2dbcConverter converter, ReactiveDataAccessStrategy accessStrategy) {
        super(entity, databaseClient, converter, accessStrategy);
        this.entity = entity;
        this.databaseClient = databaseClient;
    }

    @Override
    public <S extends T> Mono<S> insert(S objectToSave) {
        Assert.notNull(objectToSave, "Object to save must not be null!");

        return this.databaseClient.insert()
                                  .into(this.entity.getJavaType())
                                  .table(this.entity.getTableName()).using(objectToSave)
                                  // Removed ID generation since it's generated initially
                                  .map((row, rowMetadata) -> objectToSave)
                                  .first()
                                  .defaultIfEmpty(objectToSave);

    }

    @Override
    public <S extends T> Mono<S> update(S objectToSave) {
        Assert.notNull(objectToSave, "Object to save must not be null!");

        return this.databaseClient.update()
                                  .table(this.entity.getJavaType())
                                  .table(this.entity.getTableName()).using(objectToSave)
                                  .fetch().rowsUpdated().handle((rowsUpdated, sink) -> {

                if (rowsUpdated == 0) {
                    sink.error(new TransientDataAccessResourceException(
                        String.format("Failed to update table [%s]. Row with Id [%s] does not exist.",
                                      this.entity.getTableName(), this.entity.getId(objectToSave))));
                } else {
                    sink.next(objectToSave);
                }
            });
    }

}

Foo存储库:

@Repository
public interface FooRepository extends CustomReactiveCrudRepository<Foo, UUID> {}

富实体:

@Data
@Table
public class Foo {

    @Id
    private UUID      id;
    private SomeStatus someStatus;
    private Boolean   someBoolean;

}

上面的示例导致 UnsupportedOperationException:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fooRepository': Invocation of init method failed; nested exception is java.lang.UnsupportedOperationException: Query derivation not yet supported!

我应该如何以适当的方式扩展此类功能?

4

2 回答 2

0

确保您不org.springframework.data.r2dbc.repository.query.Query 导入org.springframework.data.jpa.repository.Query

于 2021-04-13T21:30:23.073 回答
0

它可能不再相关,但这是我的做法(使用 Spring webflux):

我正在使用 R2dbc,它SimpleR2dbcRepository为每个存储库创建一个。

我创建了一个自定义类,它扩展了SimpleR2dbcRepository被调用的CustomRepository. 在我说的配置中@EnableR2dbcRepositories(repositoryBaseClass = CustomRepository.class)

然后我可以覆盖旧方法并创建新方法。

于 2020-12-29T15:37:10.417 回答