spring-data-jdbc 错误 找不到 class 所需的标识符 属性

spring-data-jdbc error Required identifier property not found for class

从数据库加载数据时出现以下错误

java.lang.IllegalStateException: Required identifier property not found for class com.sudhirt.practice.springdatajdbcpractice.entity.AuthorRef!

    at org.springframework.data.mapping.PersistentEntity.getRequiredIdProperty(PersistentEntity.java:105)
    at org.springframework.data.jdbc.core.EntityRowMapper.readEntityFrom(EntityRowMapper.java:143)
    at org.springframework.data.jdbc.core.EntityRowMapper.readFrom(EntityRowMapper.java:124)
    at org.springframework.data.jdbc.core.EntityRowMapper.lambda$createInstance[=14=](EntityRowMapper.java:167)

下面是实体classAuthorRef

@Data
@Table("BOOK_AUTHOR")
@NoArgsConstructor
@AllArgsConstructor
public class AuthorRef {
    private Long author;
}

出现上述错误的原因可能是什么?

源代码位于 https://github.com/sudhirtumati/spring-data-jdbc-sample

您在聚合根 Book 内的 Set 中引用了 AuthorRef

public class Book {

    @Id
    private Long id;
    private String name;

    // ...

    private Set<AuthorRef> authorRefList;

    // ...
}

没有 id 列 Spring 数据无法确定 AuthorRef 的主键。

只需在 author 中添加一个 @Id 注释就足够了。

或者,您可以使用 List,这将添加一个额外的 book_key 列,该列与 book 列一起构成一个主键。