gpt4 book ai didi

java - 无法执行 SQL 语句

转载 作者:行者123 更新时间:2023-12-02 10:46:47 25 4
gpt4 key购买 nike

在编写我的应用程序时,我遇到了执行 SQL 语句的问题。我尝试在网上寻找解决方案,但找到的解决方案都没有帮助,而且我仍然不知道如何处理遇到的错误。这是我得到的异常:

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["FKAM8LLDERP40MVBBWCEQPU6L2S: PUBLIC.BOOK_CATEGORY FOREIGN KEY(CATEGORY_ID) REFERENCES PUBLIC.CATEGORY(ID) (2)"; SQL statement:
insert into book_category (book_id, category_id) values (?, ?) [23506-196]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

这就是类的样子:Book.class

@Entity
@Table(name = "book")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String title;
private String description;
@Column(name = "release_date")
@Temporal(TemporalType.TIMESTAMP)
private Date releaseDate;
@JoinColumn(name = "cover_image")
@OneToOne(cascade = CascadeType.MERGE)
private UploadFile coverImage;
@OneToOne(cascade = CascadeType.MERGE)
private UploadFile content;
@ManyToMany
@JoinTable(name = "book_category", joinColumns = {@JoinColumn(name = "book_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "category_id", referencedColumnName = "id")})
private Set<Category> categories;

// constructors, setters, getters

}

类别.class

@Entity
@Table(name = "category")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "category_name")
private String name;
@ManyToMany(mappedBy = "categories", fetch = FetchType.LAZY)
private List<Book> books;

// ...
}

最佳答案

您缺少 cascade 定义。

您的图书类别是在首先插入引用的实体(图书或类别)之前写入的。由于您指定的限制,这是不允许的。解决方案是定义

cascade = CascadeType.ALL

在连接的两侧(在书籍和类别中)。这将确保 JPA 以正确的顺序保留所有实体。

关于java - 无法执行 SQL 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52495476/

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