1

スプリング ブートReactiveCrudRepositoryインターフェイスを拡張して、エンティティの挿入と更新に個別のメソッドを使用するようにしてください。現時点save()では、挿入と更新のチェック ID が提供されているかどうかを区別するメソッドがあります。新しいエンティティまたは変更されたエンティティを含む今後のカフカ イベントには、すでに 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> {}

Foo エンティティ:

@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 に答える