1

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

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Sudhir
  • 1,339
  • 2
  • 15
  • 36

1 Answers1

0

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.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • Got error **Could not read property @org.springframework.data.annotation.Id()private java.lang.Long com.sudhirt.practice.springdatajdbcpractice.entity.AuthorRef.author from result set!** when annotated `author` with `@Id` annotation. Refer to sample repo commit id `18062a6` – Sudhir Aug 23 '19 at 14:34
  • When replaced set with a list, got error **Required identifier property not found for class com.sudhirt.practice.springdatajdbcpractice.entity.AuthorRef!**. Refer to commit id `47eacc8` – Sudhir Aug 23 '19 at 14:43
  • Thanks for providing the example repo. It looks like you hit one of those bugs we fixed for 1.1. With an update to the latest boot version both variants (List and Id) do work. See my PR for details: https://github.com/sudhirtumati/spring-data-jdbc-sample/pull/1 – Jens Schauder Aug 26 '19 at 06:33