3

次のようなスーパー Entity クラスがあります。

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

Role クラスは、次のように GenericEntity から拡張されます。

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

その後、次のようなインターフェイス RoleRepo があります。

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

ルーター関数には、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);
    }

メソッド 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]

でも動くと

@Id
private Long id;

GenericEntity クラスから Role クラスまで、2 つのメソッドは正常に機能します。Spring Reactive Data にそのような注釈 @MappedSuperclass/JPA はありますか

すべての extends クラスの GenericEntity に id フィールドが必要です

ご協力いただきありがとうございます

すみません、私の英語はとても下手です

4

1 に答える 1

4

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. it aggregates Role class properties with Generic class properties and then inserts all into the role table without the need to use any annotation.

于 2020-12-08T05:52:50.813 に答える