Khai Ly
Khai Ly

Reputation: 145

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:

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

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

In Router function, I have 2 handler methods

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:

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. Are there any Annations @MappedSuperclass/JPA in Spring Reactive Data like that

I wish the id field in GenericEntity for all extends class

Thanks for your help

Sorry, my English so bad

Upvotes: 3

Views: 1582

Answers (1)

AliAkbar
AliAkbar

Reputation: 61

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.

Upvotes: 6

Related Questions