Search code examples
javaspringspring-dataspring-data-jdbc

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


Getting below error while loading data from database

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$0(EntityRowMapper.java:167)

Below is the entity class AuthorRef

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

What might be the reason for above error?

Source code is available at https://github.com/sudhirtumati/spring-data-jdbc-sample


Solution

  • You are referencing AuthorRef in a Set inside your aggregate root Book.

    public class Book {
    
        @Id
        private Long id;
        private String name;
    
        // ...
    
        private Set<AuthorRef> authorRefList;
    
        // ...
    }
    

    Without an id column Spring Data can't determine a primary key for AuthorRef.

    Just adding an @Id annotation to author should be sufficient.

    Alternatively you could use a List which will add an additional book_key column which together with the book column form a primary key.