尝试使用双向多对多关联,当通过实体管理器在非关键数据列上合并执行更新时,将删除该更新记录的联接表记录。我想弄清楚如何避免生成该delete语句。
看到它在工作中发生。然后设置更简单的设置,以查看是否在那里发生了。
在给出的情况下,目标是更新当前与作者相关联的书名。
@Entity
public class Book {
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "book_author",
joinColumns = { @JoinColumn(name = "fk_book") },
inverseJoinColumns = { @JoinColumn(name = "fk_author") })
private Set<Author> authors = new HashSet<Author>();
}
@Entity
public class Author {
@ManyToMany(mappedBy="authors" )
private Set<Book> books = new HashSet<Book>();
}
//junit test
@Test
public void mergeBooks01() {
bookList = new ArrayList<Book>();
bookList = appDataAccess.findAllBooks();
for(Book b1 : bookList) {
System.out.println(b1.toString());
}
bookList = new ArrayList<Book>();
Book b = new Book();
b = new Book();
b.setId(1L);
b.setTitle("u1");
bookList.add(b);
appDataAccess.mergeBooks(bookList);
bookList = appDataAccess.findAllBooks();
for(Book b1 : bookList) {
System.out.println(b1.toString());
}
}
dao call
public void mergeBooks(List<Book> bookList) {
for(Book b : bookList) {
em.merge(b);
}
}
以下是之前的数据:
Book [id=1, title=It, authors=[Author [id=1, firstName=Stephen, lastName=King]]]
Book [id=2, title=Misery, authors=[Author [id=1, firstName=Stephen, lastName=King]]]
Book [id=3, title=The Shining, authors=[Author [id=1, firstName=Stephen, lastName=King]]]
Book [id=4, title=Watchers, authors=[Author [id=2, firstName=Dean, lastName=Koontz]]]
Book [id=5, title=Odd Thomas, authors=[Author [id=2, firstName=Dean, lastName=Koontz]]]
Book [id=6, title=Illuminae, authors=[Author [id=3, firstName=Amie, lastName=Kaufman], Author [id=4, firstName=Jay, lastName=Kristoff]]]
Book [id=7, title=Beautiful Creatures, authors=[Author [id=6, firstName=Margaret, lastName=Stohl], Author [id=5, firstName=Kami, lastName=Garcia]]]
以下是数据
Book [id=1, title=FireStarter, authors=[]]
Book [id=2, title=Misery, authors=[Author [id=1, firstName=Stephen, lastName=King]]]
Book [id=3, title=The Shining, authors=[Author [id=1, firstName=Stephen, lastName=King]]]
Book [id=4, title=Watchers, authors=[Author [id=2, firstName=Dean, lastName=Koontz]]]
Book [id=5, title=Odd Thomas, authors=[Author [id=2, firstName=Dean, lastName=Koontz]]]
Book [id=6, title=Illuminae, authors=[Author [id=3, firstName=Amie, lastName=Kaufman], Author [id=4, firstName=Jay, lastName=Kristoff]]]
Book [id=7, title=Beautiful Creatures, authors=[Author [id=5, firstName=Kami, lastName=Garcia], Author [id=6, firstName=Margaret, lastName=Stohl]]]
在Eclipse的控制台中看到
Hibernate:
select
authors0_.fk_book as fk_book2_2_0_,
authors0_.fk_author as fk_autho1_2_0_,
author1_.id as id1_0_1_,
author1_.first_name as first_na2_0_1_,
author1_.last_name as last_nam3_0_1_
from
book_author authors0_
inner join
author author1_
on authors0_.fk_author=author1_.id
where
authors0_.fk_book=?
Hibernate:
update
book
set
title=?
where
id=?
Hibernate:
delete
from
book_author
where
fk_book=?
2019-06-14 22:11:47.229 INFO 12600 --- [ main] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
问题是最后一个delete语句..为什么会发生?
在authors []中什么也没注意[]链接已删除,因此无法链接到它