如何用JPA和Hibernate删除多对多表之间的关系 [英] How to remove relationship between many to many table with JPA and Hibernate
问题描述
以下是每个表的列: 。 ID是主键。 LikedRecipe:id,姓名,电子邮件地址。 ID是主键。 我写了一个方法来删除帐户和配方之间的关联: 这个方法并没有从LikedRecipe表中删除记录,直到我添加了这一行代码在方法的末尾:
@Entity
@Table(name =Recipe)
public class Recipe {
private Set< Account>帐户;
私人长ID;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name =LikedRecipe,joinColumns = @JoinColumn(name =recipeId),inverseJoinColumns = @JoinColumn (name =email))
public Set< Account> getAccount(){
return account;
}
public void setAccount(Set< Account> account){
this.account = account;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name =id)
public Long getId(){
返回ID;
}
public void setId(Long id){
this.id = id;
@Entity
@Table(name =Account)
public class Account实现Serializable {
私人套餐<食谱> likedRecipes = new HashSet< Recipe>();
私人字符串电子邮件;
@ManyToMany(fetch = FetchType.LAZY,cascade = {CascadeType.ALL})
@JoinTable(name =LikedRecipe,joinColumns = @JoinColumn(name =email), inverseJoinColumns = @JoinColumn(name =recipeId))
public Set< Recipe> getLikedRecipes(){
return likedRecipes;
}
public void setLikedRecipes(Set< Recipe> likedRecipes){
this.likedRecipes = likedRecipes;
@Column(name =email)
@Id
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
code $
$ b $ p
public void differentARipe(String email,Long recipeId) {
Query query = entityManager
.createQuery(从LikedRecipe中删除,其中recipeId =:recipeId和email =:email);
query.setParameter(recipeId,recipeId);
query.setParameter(email,email);
query.executeUpdate();
}
$ b $ p
根据JPA API文档,clear方法清除持久性上下文,导致所有被管实体分离。
我的问题是detach是什么意思?而且,分离对象如何使上述方法从LikedRecipe表中删除记录?我是否以正确的方式使用clear方法?
谢谢。
分离的实体是一个当前没有被持久化上下文管理的实体,但其ID在数据库中。
我认为你没有将LikedRecipe实体删除因为你仍然有它从其他持久实体(帐户和食谱)引用。事实上,当你清除持久性上下文时,它会起作用,将所有你想要删除的LikedRecipe保持活跃的实体分开。
如果你想保持多对多当你要删除LikedRecipe实体时,你必须清除它们(即从Account和Recipe的集合中删除对象)。
I have two tables: Recipe table and Account table. The Recipe table stores a number of recipes. The account table stores a number of user account. A user can be associated with 0 or more recipe. When a user likes a recipe, this user is associated to the recipe. To record this association, I created a table called LikedRecipe table.
These are the columns of each table:
- Recipe: id, name. Id is the primary key.
- Account: email, password. Email is the primary key.
LikedRecipe: id, name, email. id is the primary key.
@Entity @Table(name = "Recipe") public class Recipe { private Set<Account> account; private Long id; @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "LikedRecipe", joinColumns = @JoinColumn(name = "recipeId"), inverseJoinColumns = @JoinColumn(name = "email")) public Set<Account> getAccount() { return account; } public void setAccount(Set<Account> account) { this.account = account; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id") public Long getId() { return id; } public void setId(Long id) { this.id = id; } } @Entity @Table(name = "Account") public class Account implements Serializable { private Set<Recipe> likedRecipes = new HashSet<Recipe>(); private String email; @ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL }) @JoinTable(name = "LikedRecipe", joinColumns = @JoinColumn(name = "email"), inverseJoinColumns = @JoinColumn(name = "recipeId")) public Set<Recipe> getLikedRecipes() { return likedRecipes; } public void setLikedRecipes(Set<Recipe> likedRecipes) { this.likedRecipes = likedRecipes; } @Column(name = "email") @Id public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
I wrote a method to remove the association between an account and a recipe:
public void unlikeARecipe(String email, Long recipeId){
Query query = entityManager
.createQuery("delete from LikedRecipe where recipeId = :recipeId and email = :email");
query.setParameter("recipeId", recipeId);
query.setParameter("email", email);
query.executeUpdate();
}
This method did not delete records from LikedRecipe table, until I added this line of code at the end of the method:
entityManager.clear();
According to JPA API documentation the clear method clears the persistence context, causing all managed entities to become detached.
My question is what does detach means ? And, how does detaching objects made the above method deletes records from LikedRecipe table? Am I using the clear method in the right manner ?
Thank you.
Detached entity is an entity not currently managed by a persistence context but whose id is present in database.
I think you don't get the LikedRecipe entity deleted because you still have it referenced from other persistent entities (Account and Recipe). Indeed it works when you clear the persistence context, detaching all entities that are "keeping alive" the LikedRecipe you wanted to delete.
If you want to keep the many-to-many relationships, you have to clear them as well (i.e. removing the object from Account's and Recipe's collections) when you're about to deleting the LikedRecipe entity.
这篇关于如何用JPA和Hibernate删除多对多表之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!