简体   繁体   English

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

[英]Are there @MappedSuperclass in Spring Reactive Data (R2DBC)

I have a super Entity class like this:我有一个像这样的超级实体类:

@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;
}

and a Role class extends from GenericEntity like this:和 Role 类从 GenericEntity 扩展,如下所示:

@Getter
@Setter
@NoArgsConstructor
public class Role extends GenericEntity {
    private String name;
    private String desc;
    private Integer sort;
}

And after that I have interface RoleRepo like this:之后我有这样的接口 RoleRepo:

@Repository
public interface RoleRepo extends ReactiveCrudRepository<Role, Long>;

In Router function, I have 2 handler methods在路由器功能中,我有 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);
    }

The method findAllHandler works fine, but the saveOrUpdateHandler throw exception like this:方法 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]

But when I move但是当我移动

@Id
private Long id;

from GenericEntity class to Role class, the two methods work fine.从 GenericEntity 类到 Role 类,这两种方法都可以正常工作。 Are there any Annations @MappedSuperclass/JPA in Spring Reactive Data like that在 Spring Reactive Data 中是否有任何 Annation @MappedSuperclass/JPA 这样的

I wish the id field in GenericEntity for all extends class我希望 GenericEntity 中的 id 字段适用于所有扩展类

Thanks for your help谢谢你的帮助

Sorry, my English so bad对不起,我的英语太差了

I had a similar problem and after some search, I didn't find an answer to your question, so I test it by writing code and the answer is spring data R2DBC doesn't need @Mappedsuperclass .我遇到了类似的问题,经过一番搜索,我没有找到您问题的答案,所以我通过编写代码对其进行了测试,答案是 spring 数据 R2DBC 不需要@Mappedsuperclass it aggregates Role class properties with Generic class properties and then inserts all into the role table without the need to use any annotation.它将Role类属性与Generic类属性聚合,然后将所有内容插入到role表中,无需使用任何注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM