英文:
Many to Many Spring Jpa Delete does not work
问题
我是新手使用Spring JPA,正在尝试执行Spring JPA多对多数据库上的删除操作。我的数据库中有用户和药物。我可以删除一个用户并从user_drug表中删除与之关联的行,我可以删除一个没有与user_drug表关联的药物,但我无法删除也存在于user_drug表中的药物。
我查看了这个页面,但那里的解决方案对我不起作用:https://stackoverflow.com/questions/1082095/how-to-remove-entity-with-manytomany-relationship-in-jpa-and-corresponding-join
这是我的User实体的代码:
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "user_drug",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "drug_id", referencedColumnName = "id"))
private Set<Drug> drugs = new HashSet<>();
这是Drug实体的代码:
@ManyToMany(mappedBy = "drugs", fetch = FetchType.EAGER)
private Set<User> users = new HashSet<>();
这是DrugServiceImpl中的删除方法:
public void delete(Drug drug)
{
drug.getUsers().clear();
drugRepository.delete(drug);
}
我还在清除操作后打印了drug.getUsers()的大小,结果为0。为什么它不会从数据库中删除药物呢?
我已经尝试了很多种方法...有人能帮忙吗?
英文:
I am new with Spring jpa and I am trying to perform the deletion operation on spring jpa many to many database. My database has user and drug. I can delete an user and deletes also the rows associated to it from user_drug table, I can delete a drug that has no linking to the user_drug table, but i cannot delete a drug that is also in the user_drug table.
I had a look on this page but the solutions from there do not work for me.. https://stackoverflow.com/questions/1082095/how-to-remove-entity-with-manytomany-relationship-in-jpa-and-corresponding-join
Here is my code for User entity:
@ManyToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable(name = "user_drug",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "drug_id", referencedColumnName = "id"))
private Set<Drug> drugs = new HashSet<>();
Here is the code for Drug entity:
@ManyToMany(mappedBy = "drugs", fetch=FetchType.EAGER)
private Set<User> users = new HashSet<>();
And here is the delet method from DrugServiceImpl:
public void delete(Drug drug)
{
drug.getUsers().clear();
drugRepository.delete(drug);
}
I have also printed the size of drug.getUsers() after clear operation and it is 0. Why isn't it delete the drug from database?
I've tried in many ways.. Can someone please send some help?
答案1
得分: 0
以下是翻译好的部分:
这就是级联的工作方式,它仅从关系表中删除,并且在“ManyToMany”中,这里的关系表是“user_drug”表。
对于这些情况,有许多解决方案之一是在同一事务中删除“user”的同时删除“drug”。
但另一个解决方案可以节省创建“ManyToMany”表的工作量,并在关系表中对“drug”对象进行级联,像这样:
但这在“User”实体中是这样的:
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "user")
private Set<UserDrug> drugs = new HashSet<>();
“UserDrug”实体如下:
public class UserDrug {
@ManyToOne
@JoinColumn(name = "user_id")
User user;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "drug_id")
Drug drug;
}
英文:
that is how the cascade works it deletes from the relation table only and in the ManyToMany
the relation table here is the user_drug
table
and for this cases there are many solution one of them is deleting the drug
in the same transaction of the deleting user
but another solution that will save the effort that you create the ManyToMany
table as entity and put cascade on the drug
object in the relation table
like this
But this in the User
entity
@ManyToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="user")
private Set<UserDrug> drugs = new HashSet<>();
the UserDrug
entity
public class UserDrug {
@ManyToOne
@JoinColumn(name="user_id")
User user;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="drug_id")
Drug drug;
}
专注分享java语言的经验与见解,让所有开发者获益!
评论