gpt4 book ai didi

java - ManyToMany Hibernate保存相关对象

转载 作者:行者123 更新时间:2023-12-01 16:41:32 25 4
gpt4 key购买 nike

我正在学习 Spring JPA,我从将对象映射到表开始。我对 OneToOne 和 OneToMany 关系没有任何问题,但我不太明白为什么我无法保留与 ManyToMany 关系相关的对象。我有一本可以有很多作者的书和一个可以有很多书的作者。模式是使用中间表 book_author 直接生成的(我可以在 MySql Workbench 中看到它),但我得到了这个异常(exception):

Hibernate:插入作者(名字,姓氏)值(?,?)

Hibernate:插入书籍(种类,标题)值(?,?)

Hibernate:插入书籍(种类,标题)值(?,?)

Hibernate:插入author_book(author_id,book_id)值(?,?)

Hibernate:插入author_book(author_id,book_id)值(?,?)

Hibernate:插入作者(名字,姓氏)值(?,?)

线程“main”中的异常org.springframework.dao.InvalidDataAccessApiUsageException:传递给持久化的分离实体:com.example.demo.models.Book;嵌套异常是 org.hibernate.PersistentObjectException:传递给持久化的分离实体:com.example.demo.models.Book

我还粘贴了 Hibernate 操作,效果很好。

这些是我的实体定义:

@Table(name = "author")
public class Author {

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

@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.ALL})
@JoinTable(
name = "author_book",
joinColumns = {@JoinColumn(name = "author_id")},
inverseJoinColumns = {@JoinColumn(name = "book_id")}
)
private List<Book> books = new ArrayList<Book>();

//getters and setters omitted for brevity
}

@Entity(name = "Book")
@Table(name = "book")
public class Book {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String title;
private String kind;
@ManyToMany(mappedBy = "books")
private List<Author> authors = new ArrayList<Author>();

//getters and setters omitted for brevity
}

这些是我的存储库:

@Repository
public interface AuthorRepository extends CrudRepository<Author, Long> {

}

@Repository
public interface BookRepository extends CrudRepository<Book, Long> {

}

最后这是我的主要内容

public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);
AuthorRepository authorRepository = ctx.getBean(AuthorRepository.class);
BookRepository bookRepository = ctx.getBean(BookRepository.class);

Author author = new Author();
author.setFirstName("Author");
author.setLastName("First");

Author author2 = new Author();
author.setFirstName("Author");
author.setLastName("Second");

Book book1 = new Book();
book1.setTitle("Book 1");
book1.setKind("History");
book1.getAuthors().add(author);
book1.getAuthors().add(author2);

Book book2 = new Book();
book2.setTitle("Book 2");
book2.setKind("Science");
book2.getAuthors().add(author);

author.getBooks().add(book1);
author.getBooks().add(book2);
author2.getBooks().add(book1);

authorRepository.save(author);
authorRepository.save(author2);

System.out.println("-->End of db operation");

}

最佳答案

Author 类中创建一个新方法来添加 book,而不是使用 author.getBooks().add(book1)

    public void addBook(Book book) {
books.add(book);
books.authors.add(this);
}

而且您也不应该使用CascadeType.ALL,因为删除实体时关系的另一端也会被删除

关于java - ManyToMany Hibernate保存相关对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61850575/

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