Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

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

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

@Service
public class BookServiceImpl implements BookService
{
@Autowired
private BookRepository bookRepository;

@Override
public Page<Book> findPaginated(int pageN, int pageSize,String sortField,String sortDirection) {
    Sort sort = sortDirection.equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(sortField).ascending() :
            Sort.by(sortField).descending();
    Pageable pageable = PageRequest.of(pageN-1,pageSize,sort);
    return bookRepository.findAll(pageable);
}

}

LibrarianController class:

@GetMapping("/books/{pageN}")
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("currentPage",pageN);
    model.addAttribute("totalPages",page.getTotalPages());
    model.addAttribute("totalItems", page.getTotalElements());
    model.addAttribute("books",books);
    model.addAttribute("sortField",sortField);
    model.addAttribute("sortDir",sortDir);
    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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading