Spring Reactive Data (R2DBC) 中是否有 @MappedSuperclass

Kha*_* Ly 3 spring reactive r2dbc

我有一个像这样的超级实体类:

@Getter
@Setter
@NoArgsConstructor
public class GenericEntity {
    @Id
    private Long id;

    @JsonIgnore
    @CreatedBy
    private Long createdBy;

    @JsonIgnore
    @CreatedDate
    private Long createdDate;

    @JsonIgnore
    @LastModifiedBy
    private Long updatedBy;

    @JsonIgnore
    @LastModifiedDate
    private Long updatedDate;

    @JsonIgnore
    @Version
    private Integer version = 0;
}
Run Code Online (Sandbox Code Playgroud)

Role 类从 GenericEntity 扩展,如下所示:

@Getter
@Setter
@NoArgsConstructor
public class Role extends GenericEntity {
    private String name;
    private String desc;
    private Integer sort;
}
Run Code Online (Sandbox Code Playgroud)

之后我有像这样的 RoleRepo 接口:

@Repository
public interface RoleRepo extends ReactiveCrudRepository<Role, Long>;
Run Code Online (Sandbox Code Playgroud)

在 Router 函数中,我有 2 个处理程序方法

private Mono<ServerResponse> findAllHandler(ServerRequest request) {
        return ok()
            .contentType(MediaType.APPLICATION_JSON)
            .body(roleRepo.findAll(), Role.class);

    }

private Mono<ServerResponse> saveOrUpdateHandler(ServerRequest request) {
        return ok()
            .contentType(MediaType.APPLICATION_JSON_UTF8)
            .body(request.bodyToMono(Role.class).flatMap(role -> {
                return roleRepo.save(role);
            }), Role.class);
    }
Run Code Online (Sandbox Code Playgroud)

findAllHandler 方法工作正常,但 saveOrUpdateHandler 抛出如下异常:

java.lang.IllegalStateException: Required identifier property not found for class org.sky.entity.system.Role!
    at org.springframework.data.mapping.PersistentEntity.getRequiredIdProperty(PersistentEntity.java:105) ~[spring-data-commons-2.2.0.M2.jar:2.2.0.M2]
    at org.springframework.data.r2dbc.function.convert.MappingR2dbcConverter.lambda$populateIdIfNecessary$0(MappingR2dbcConverter.java:85) ~[spring-data-r2dbc-1.0.0.M1.jar:1.0.0.M1]
Run Code Online (Sandbox Code Playgroud)

但当我移动时

@Id
private Long id;
Run Code Online (Sandbox Code Playgroud)

从GenericEntity类到Role类,这两种方法都可以正常工作。Spring Reactive Data 中是否有类似的 Annations @MappedSuperclass/JPA

我希望 GenericEntity 中的 id 字段适用于所有扩展类

感谢您的帮助

抱歉,我的英语太差了

小智 6

我遇到了类似的问题,经过一番搜索,我没有找到你问题的答案,所以我通过编写代码进行测试,答案是 spring data R2DBC does not need @Mappedsuperclass。它将Role类属性与Generic类属性聚合,然后将所有内容插入role表中,而无需使用任何注释。