扩展反应堆库

尝试扩展spring boot ReactiveCrudRepository接口,以具有用于实体插入和更新的单独方法。现在,是否提供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);
                }
            });
    }

}

FooRepository:

@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!

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

sdaSDWDADW 回答:扩展反应堆库

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

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

我创建了一个自定义类,它扩展了名为 SimpleR2dbcRepositoryCustomRepository。在配置中我说 @EnableR2dbcRepositories(repositoryBaseClass = CustomRepository.class)

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

,

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

本文链接:https://www.f2er.com/2899252.html

大家都在问