Deleting Error

i am tryna delete a book from a list of books but end up with this error
9 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @Danix! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Danix
DanixOP2mo ago
No description
Danix
DanixOP2mo ago
This is the place where i was deleting it
<form th:action="@{/admin/deletestudentbook}" method="post" style="display:inline;">
<input type="hidden" name="roll_no" th:value="${studentAllBooks.roll_no}" />
<input type="hidden" name="book_id" th:value="${book.bookId}" />
<button type="submit" class="absolute top-2 right-2 text-gray-500 hover:text-red-500">
<i class="fas fa-trash-alt"></i> <!-- Dustbin Icon -->
</button>
</form>
<form th:action="@{/admin/deletestudentbook}" method="post" style="display:inline;">
<input type="hidden" name="roll_no" th:value="${studentAllBooks.roll_no}" />
<input type="hidden" name="book_id" th:value="${book.bookId}" />
<button type="submit" class="absolute top-2 right-2 text-gray-500 hover:text-red-500">
<i class="fas fa-trash-alt"></i> <!-- Dustbin Icon -->
</button>
</form>
This is the endpoint
@PostMapping("/deletestudentbook")
public String deleteStudentBook(@RequestParam Long roll_no, Long book_id, Model model) {
log.debug("Received request to delete student book. roll_no: {}, book_id: {}", roll_no, book_id);

StudentDetailsDto studentDetails = studentDetailsService.getStudentById(roll_no)
.orElseThrow(() -> {
log.error("Student not found for roll_no: {}", roll_no);
return new StudentNotFound("No Student Found");
});

model.addAttribute("studentAllBooks", studentDetails);

if (studentDetails.getBooksDetails().size() == 1) {
log.debug("Deleting student books and details. roll_no: {}", roll_no);
studentDetailsService.deleteStudentBooks(roll_no, book_id);
studentDetailsService.deleteStudentDetails(roll_no);
return "redirect:/";
} else {
log.debug("Deleting student books. roll_no: {}", roll_no);
studentDetailsService.deleteStudentBooks(roll_no, book_id);
return "allbooks";
}

}
}
@PostMapping("/deletestudentbook")
public String deleteStudentBook(@RequestParam Long roll_no, Long book_id, Model model) {
log.debug("Received request to delete student book. roll_no: {}, book_id: {}", roll_no, book_id);

StudentDetailsDto studentDetails = studentDetailsService.getStudentById(roll_no)
.orElseThrow(() -> {
log.error("Student not found for roll_no: {}", roll_no);
return new StudentNotFound("No Student Found");
});

model.addAttribute("studentAllBooks", studentDetails);

if (studentDetails.getBooksDetails().size() == 1) {
log.debug("Deleting student books and details. roll_no: {}", roll_no);
studentDetailsService.deleteStudentBooks(roll_no, book_id);
studentDetailsService.deleteStudentDetails(roll_no);
return "redirect:/";
} else {
log.debug("Deleting student books. roll_no: {}", roll_no);
studentDetailsService.deleteStudentBooks(roll_no, book_id);
return "allbooks";
}

}
}
Danix
DanixOP2mo ago
This is the UI result
No description
Danix
DanixOP2mo ago
I dont know why this error
llers.AdminController#studentAllBooks(Long, Model)
2024-09-28T20:27:57.996+05:30 DEBUG 6724 --- [nio-8080-exec-1] org.hibernate.SQL : select sd1_0.roll_no,sd1_0.email,sd1_0.name,sd1_0.phone_number from student_details sd1_0 where sd1_0.roll_no=?
Hibernate: select sd1_0.roll_no,sd1_0.email,sd1_0.name,sd1_0.phone_number from student_details sd1_0 where sd1_0.roll_no=?
2024-09-28T20:27:58.002+05:30 DEBUG 6724 --- [nio-8080-exec-1] org.hibernate.SQL : select bd1_0.user_roll_no,bd1_0.book_id,bd1_0.authors,bd1_0.book_name,bd1_0.book_pdf,bd1_0.currency_code,bd1_0.description,bd1_0.image,bd1_0.issue_date,bd1_0.price,bd1_0.publish_date,bd1_0.return_date,bd1_0.subtitle,bd1_0.synposis from book_details bd1_0 where bd1_0.user_roll_no=?
Hibernate: select bd1_0.user_roll_no,bd1_0.book_id,bd1_0.authors,bd1_0.book_name,bd1_0.book_pdf,bd1_0.currency_code,bd1_0.description,bd1_0.image,bd1_0.issue_date,bd1_0.price,bd1_0.publish_date,bd1_0.return_date,bd1_0.subtitle,bd1_0.synposis from book_details bd1_0 where bd1_0.user_roll_no=?
2024-09-28T20:27:58.005+05:30 DEBUG 6724 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8, application/signed-exchange;v=b3;q=0.7]
2024-09-28T20:27:58.009+05:30 DEBUG 6724 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 200 OK
llers.AdminController#studentAllBooks(Long, Model)
2024-09-28T20:27:57.996+05:30 DEBUG 6724 --- [nio-8080-exec-1] org.hibernate.SQL : select sd1_0.roll_no,sd1_0.email,sd1_0.name,sd1_0.phone_number from student_details sd1_0 where sd1_0.roll_no=?
Hibernate: select sd1_0.roll_no,sd1_0.email,sd1_0.name,sd1_0.phone_number from student_details sd1_0 where sd1_0.roll_no=?
2024-09-28T20:27:58.002+05:30 DEBUG 6724 --- [nio-8080-exec-1] org.hibernate.SQL : select bd1_0.user_roll_no,bd1_0.book_id,bd1_0.authors,bd1_0.book_name,bd1_0.book_pdf,bd1_0.currency_code,bd1_0.description,bd1_0.image,bd1_0.issue_date,bd1_0.price,bd1_0.publish_date,bd1_0.return_date,bd1_0.subtitle,bd1_0.synposis from book_details bd1_0 where bd1_0.user_roll_no=?
Hibernate: select bd1_0.user_roll_no,bd1_0.book_id,bd1_0.authors,bd1_0.book_name,bd1_0.book_pdf,bd1_0.currency_code,bd1_0.description,bd1_0.image,bd1_0.issue_date,bd1_0.price,bd1_0.publish_date,bd1_0.return_date,bd1_0.subtitle,bd1_0.synposis from book_details bd1_0 where bd1_0.user_roll_no=?
2024-09-28T20:27:58.005+05:30 DEBUG 6724 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8, application/signed-exchange;v=b3;q=0.7]
2024-09-28T20:27:58.009+05:30 DEBUG 6724 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 200 OK
Can anyone help
straightface
straightface2mo ago
what happens in studentDetailsService#deleteStudentBooks? @Danix
Danix
DanixOP2mo ago
@Override
public Optional<StudentDetails> getStudentDetailsByIdForDeleting(Long roll_no) {
Optional<StudentDetails> studentDetails = detailsRepository.findById(roll_no);
if (studentDetails.isPresent()) {
StudentDetails studentDetailsOptional = studentDetails.get();
return Optional.of(studentDetailsOptional);
}
return Optional.empty();
}
@Override
public Optional<StudentDetails> getStudentDetailsByIdForDeleting(Long roll_no) {
Optional<StudentDetails> studentDetails = detailsRepository.findById(roll_no);
if (studentDetails.isPresent()) {
StudentDetails studentDetailsOptional = studentDetails.get();
return Optional.of(studentDetailsOptional);
}
return Optional.empty();
}
@GAS not liquid
straightface
straightface2mo ago
hmm, not what i asked for is it now. error states that you are trying to set phone_number col as null, which is not possible due to a constraint you set in the database
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server