How can i resolve the error Cannot cast java.lang.Integer to java.lang.String?

Discussão em 'Angular' iniciado por Steve, Outubro 29, 2024.

  1. Steve

    Steve Guest

    I am implementing the process of borrowing a book using angular and springboot but I am getting the error Cannot cast java.lang.Integer to java.lang.String.

    Here is my Angular code:

    borrowBook(book: BookResponse) {
    this.message = "";
    this.bookService.borrowBook({
    'book-id': book.id as number
    }).subscribe({
    next: () => {
    this.level = 'success';
    this.message = "Book successfully added to your list";
    },
    error: (err) => {
    console.log(err);
    this.level = 'error';
    this.message = err.error.error;
    }
    });
    }


    In Spring boot, this is my controller method :

    @PostMapping("borrow/{book-id}")
    public ResponseEntity<Integer> borrowBook(
    @PathVariable("book-id") Integer bookId,
    Authentication connectedUser
    ) {
    return ResponseEntity.ok(bookService.borrowBook(bookId, connectedUser));
    }


    The logic is as below:

    public Integer borrowBook(Integer bookId, Authentication connectedUser) {
    Book book = bookRepository.findById(bookId)
    .orElseThrow(() -> new EntityNotFoundException("No book found with ID:: " + bookId));
    if (book.isArchived() || !book.isShareable()) {
    throw new OperationNotPermittedException("The requested book cannot be borrowed since it is archived or not shareable");
    }
    User user = ((User) connectedUser.getPrincipal());
    if (Objects.equals(book.getOwner().getUser_id(), user.getUser_id())) {
    throw new OperationNotPermittedException("You cannot borrow your own book");
    }

    System.out.println("this works");
    final boolean isAlreadyBorrowedByUser = transactionHistoryRepository.isAlreadyBorrowedByUser(bookId, user.getUser_id());
    if (isAlreadyBorrowedByUser) {
    throw new OperationNotPermittedException("You already borrowed this book and it is still not returned or the return is not approved by the owner");
    }
    final boolean isAlreadyBorrowedByOtherUser = transactionHistoryRepository.isAlreadyBorrowed(bookId);
    if (isAlreadyBorrowedByOtherUser) {
    throw new OperationNotPermittedException("The requested book is already borrowed");
    }

    BookTransactionHistory bookTransactionHistory = BookTransactionHistory.builder()
    .user(user)
    .book(book)
    .isReturned(false)
    .isReturnApproved(false)
    .build();
    return transactionHistoryRepository.save(bookTransactionHistory).getId();

    }


    Here is my repository :

    @Query("""
    SELECT CASE WHEN COUNT(bookTransactionHistory) > 0 THEN true ELSE false END
    FROM BookTransactionHistory bookTransactionHistory
    WHERE bookTransactionHistory.user.user_id = :userId
    AND bookTransactionHistory.book.id = :bookId
    AND bookTransactionHistory.isReturnApproved = false
    """)
    boolean isAlreadyBorrowedByUser(@Param("bookId") Integer bookId, @Param("userId") Integer userId);

    @Query("""
    SELECT CASE WHEN COUNT(bookTransactionHistory) > 0 THEN true ELSE false END
    FROM BookTransactionHistory bookTransactionHistory
    WHERE bookTransactionHistory.book.id = :bookId
    AND bookTransactionHistory.isReturnApproved = false
    """)
    boolean isAlreadyBorrowed(@Param("bookId") Integer bookId);


    And the book entity is as follows:

    @Getter
    @Setter
    @SuperBuilder
    @AllArgsConstructor
    @NoArgsConstructor
    @Entity
    public class BookTransactionHistory extends BaseEntity {

    // user relationship
    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    // book relationship
    @ManyToOne
    @JoinColumn(name = "book_id")
    private Book book;


    private boolean isReturned;
    private boolean isReturnApproved;
    }


    The above code is expected to report errors when a user borrows a book they own, they have already borrowed or have been borrowed by other users, otherwise return the id of the borrowed book. However, I am experiencing the above error, and I am thinking it has to do with the @Query in the repository. I have tried changing the borrowBook() return type to string but then i would have to return another field other than the id.

    Continue reading...

Compartilhe esta Página