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

标签 java spring hibernate

我正在尝试编写一种方法,从表 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/

相关文章:

java - 使用 spring 注释自动检测组件

database - 使用 C3P0 时如何最好地关闭连接并避免非事件 session ?

java - 在 xml 配置文件中找不到类 [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]

java - Hibernate 中的 BigInteger 映射应该使用什么列类型?

java - 如何获取 UnexpectedRollbackException 的 SQL 错误来源

java - 为什么我们不能手动迭代 LinkedList?

java - 在java中,动态分配

java - 当用户添加gradle依赖项时如何使我的Android库源代码可见(因此不必在Android Studio中反编译代码)

java - 异常处理状态码

java - 连接的 Flex 客户端与 GraniteDS 教程中的数据更新不同步