gpt4 book ai didi

java - 从关系数据库中删除实体时出现问题

转载 作者:行者123 更新时间:2023-12-02 08:59:01 27 4
gpt4 key购买 nike

我正在尝试编写一种方法,从表 book 中删除与表 comments 中的实体有关系的实体。这个想法是删除 comments 中的所有记录,同时删除 book 中的记录。

有人可以帮我解决这个问题吗?我不知道出了什么问题,它正确删除了注释,但随后发生了错误。负责从 Spring 删除、类和消息的服务方法如下。干杯

错误如下:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find birski.bookstore.models.Comment with id 1; nested exception is javax.persistence.EntityNotFoundException: Unable to find birski.bookstore.models.

使用 id 1] 进行评论并说明根本原因

负责删除实体的服务方法

    public ResponseEntity<?> deleteBookDto(String bookTitle){
return bookRepository.findByTitle(bookTitle).map(b ->{
for (Comment comment : b.getComments()){
commentRepository.delete(comment);
}
bookRepository.delete(b);
return new ResponseEntity<>("Book titled: " + bookTitle + " was deleted!", HttpStatus.OK);
}).orElseThrow(()-> new ResourceNotFoundException("Book titled: " + bookTitle + " have not been found."));
}

图书类(class)

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

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotBlank(message = "Title is required")
private String title;

@NotBlank(message = "Author is required")
private String author;

@OneToMany(fetch = FetchType.EAGER, mappedBy = "book")
@OnDelete(action = OnDeleteAction.CASCADE)
private Set<Comment> comments = new HashSet<>();

public Book() {
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public Set<Comment> getComments() {
return comments;
}

public void setComments(Set<Comment> comments) {
this.comments = comments;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}

评论类

@Entity
@Table(name = "comments", schema = "public")
public class Comment {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
private String author;

@NotNull
private String description;

@NotNull
private float rating;

@NotNull
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private Date date;

@ManyToOne//(cascade = CascadeType.ALL)
private Book book;

public Comment() {
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public float getRating() {
return rating;
}

public void setRating(float rating) {
this.rating = rating;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public Book getBook() {
return book;
}

public void setBook(Book book) {
this.book = book;
}
}

最佳答案

你有

@OneToMany(fetch = FetchType.EAGER, mappedBy = "book")
@OnDelete(action = OnDeleteAction.CASCADE)
private Set<Comment> comments = new HashSet<>();

以及显式delete()您的评论的代码。当 Hibernate 处理删除书籍时,它会尝试删除书籍的评论,但您已经从数据库中删除了这些评论。您需要其中之一。

关于java - 从关系数据库中删除实体时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60310867/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com