javax.persistence.EntityNotFoundException: Unable to find kg.library.spring.library_spring.entity.Author with id 10000001


I’m new to Spring and I’m probably making the dumbest mistake, but I can’t solve this problem for more than 2 hours. According to the video tutorial, I did Pagination, I did it exactly like his, but he did not have relationships between entities. I think the error is in a one-to-one relationship between Author and Book entity. Can you please help?

I wanted to add pagination because I have more than a million records in my table, after adding pagination I got this error.

Book Entity:

@Table(name = "books")
public class Book {

@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@Column(name = "title")
private String title;

@Column(name = "publishing_year")
private Integer publishingYear;

@Column(name = "sell_cost")
private BigDecimal sellCost;

@Column(name = "rent_cost")
private BigDecimal rentCost;

@Column(name = "amount")
private Integer amount;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "author_id")
private Author author;

//getter and setters

Author Entity:

@Table(name = "author")
public class Author {

@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@Column(name = "fullname")
private String fullname;

@Column(name = "birthday")
private Date birthday;

@OneToOne(mappedBy = "author")
private Book book;

//getters and setters

BookServiceImpl class:

public class BookServiceImpl implements BookService
private BookRepository bookRepository;

public Page<Book> findPaginated(int pageN, int pageSize,String sortField,String sortDirection) {
    Sort sort = sortDirection.equalsIgnoreCase( ? :
    Pageable pageable = PageRequest.of(pageN-1,pageSize,sort);
    return bookRepository.findAll(pageable);


LibrarianController class:

public String getAllBooks(@PathVariable (value = "pageN") int pageN,Model model,
                          @RequestParam("sortField") String sortField,
                          @RequestParam("sortDir") String sortDir){

    int pageSize = 25;

    Page<Book> page = bookService.findPaginated(pageN,pageSize,sortField,sortDir);
    List<Book> books = page.getContent();

    model.addAttribute("totalItems", page.getTotalElements());
    model.addAttribute("reverseSortDir",sortDir.equals("asc") ? "desc" : "asc");
    return "librarian/show-all-books";

>Solution :

It seems that you have a Book record that refers to an Author with id 10000001 that does not exit in Author table.

Leave a ReplyCancel reply