[英]JPA - Nothing happens after merge
Someone can tell me why nothing happens when I merge entity? 有人可以告诉我为什么合并实体时什么也没发生?
User.java (Entity) User.java(实体)
@ManyToMany(mappedBy="users", targetEntity=Books.class,cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
private List<Books> books;
Books.java (Entity) Books.java(实体)
@ManyToMany (targetEntity = User.class,cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
private List<User> users;
returnBookServlet.java returnBookServlet.java
int userId = (int) session.getAttribute("userId");
String stringIdBook = request.getParameter("idBook");
UserDAO daoUser = (UserDAO) request.getAttribute("userDao");
User user = daoUser.getUser(userId);
List<Books> booksOrderedByUser = user.getBooks();
for (Books x : booksOrderedByUser) {
String idBookString = Integer.toString(x.getIdBook());
if (idBookString.equals(stringIdBook)) {
booksOrderedByUser.remove(x);
break;
}
}
user.setBooks(booksOrderedByUser);
entityManager.getTransaction().begin();
entityManager.merge(user);
entityManager.flush();
entityManager.getTransaction().commit();
entityManager.close();
After and before foreach loop I display list via System.out.println()
and it's correctly remove choosen book but nothing happen in database, what I did wrong? 在foreach循环之前和之后,我通过System.out.println()
显示列表,它可以正确删除所选的书,但是数据库中什么也没有发生,我做错了什么?
The changes done in User entity are not synchronized with your data base because User entity is the non-owning side of your bidirectional ManyToMany relationship. User实体中所做的更改不会与您的数据库同步,因为User实体是双向ManyToMany关系的非所有者方。 You are using mappedby="users" in User entity, so the owner side is Book entity. 您正在使用User实体中的mappingby =“ users”,因此所有者端是Book实体。
Your code should work if you change the owning side of your ManyToMany, in ManyToMany bidirectional relationships you can choose which side is your owning side (from JPA 2.1 specification): 如果您更改ManyToMany的拥有方,则代码应该可以工作,在ManyToMany双向关系中,您可以选择哪一方是您的拥有方(根据JPA 2.1规范):
Every many-to-many association has two sides, the owning side and the non-owning, or inverse, side. 每个多对多关联都有两个方面,即拥有方和非拥有方或反向方。 If the association is bidirectional, either side may be designated as the owning side. 如果关联是双向的,则任何一方都可以指定为拥有方。 If the relationship is bidirectional, the non-owning side must use the mappedBy element of the ManyToMany annotation to specify the relationship field or property of the owning side. 如果关系是双向的,则非拥有方必须使用ManyToMany批注中的mappingBy元素来指定拥有方的关系字段或属性。
Then, if you want to use the same code, change your anotations to: 然后,如果要使用相同的代码,请将注释更改为:
User.java (Entity) User.java(实体)
@ManyToMany(targetEntity=Books.class,cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
private List<Books> books;
Books.java (Entity) Books.java(实体)
@ManyToMany (mappedBy="books",targetEntity = User.class,cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
private List<User> users;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.