Home > Mobile >  javax.persistence.EntityNotFoundException: Unable to find kg.library.spring.library_spring.entity.Au
javax.persistence.EntityNotFoundException: Unable to find kg.library.spring.library_spring.entity.Au

Time:05-16

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:

@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";
}

CodePudding user response:

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

CodePudding user response:

Try these changes. I hope that each book has only one author.

Book.java:

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

Author.java:

    @OneToMany(mappedBy = "author")
    private List<Book> books=new ArrayList<>();
  • Related