如何用JPA和Hibernate删除多对多表之间的关系 [英] How to remove relationship between many to many table with JPA and Hibernate

查看:1228
本文介绍了如何用JPA和Hibernate删除多对多表之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表:配方表和帐户表。食谱表存储了一些食谱。账户表存储了一些用户账户。用户可以与0个或更多配方相关联。当用户喜欢配方时,此用户与配方关联。为了记录这个关联,我创建了一个名为LikedRecipe表的表。



以下是每个表的列: 。 ID是主键。

  • 帐户:电子邮件,密码。电子邮件是主键。

  • LikedRecipe:id,姓名,电子邮件地址。 ID是主键。

      @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();
    }

    这个方法并没有从LikedRecipe表中删除记录,直到我添加了这一行代码在方法的末尾:
    $ b $ p entityManager.clear();



    根据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屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆