休眠:删除子对象

问题描述:

我有以下POJO类:休眠:删除子对象

@Entity 
@Table(name = "category") 
public final class Category extends AbstractData<Category> 
{ 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Integer categoryID; 

    @Column(name = "categoryName") 
    private String categoryName; 

    @Column(name = "categoryDescription") 
    private String categoryDescription; 

    @ManyToOne(cascade = CascadeType.ALL, fetch=FetchType.EAGER) 
    @JoinColumn(name = "parentCategoryID", nullable = true) 
    private Category parentCategory; 

    @OneToMany(mappedBy = "parentCategory") 
    private Set<Category> subCategories; 

    @ManyToMany(cascade = 
    { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "categories") 
    private Set<Book> books; 

    public Category() 
    { 
     this("", "", null); 
    } 

    public Category(String name, String description) 
    { 
     this(name, description, null); 
    } 

    public Category(String name, String description, Category parent) 
    { 
     this.categoryName = name; 
     this.categoryDescription = description; 
     this.parentCategory = parent; 

     subCategories = new HashSet<Category>(); 
     books = new HashSet<Book>(); 
    }  

    @Override 
    public int hashCode() 
    { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((categoryDescription == null) ? 0 : categoryDescription.hashCode()); 
     result = prime * result + ((categoryName == null) ? 0 : categoryName.hashCode()); 
     result = prime * result + ((parentCategory == null) ? 0 : parentCategory.hashCode()); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) 
    { 
     if (this == obj) 
     { 
      return true; 
     } 

     if (obj == null) 
     { 
      return false; 
     } 

     if (getClass() != obj.getClass()) 
     { 
      return false; 
     } 

     Category other = (Category) obj; 

     if (categoryName == null) 
     { 
      if (other.categoryName != null) return false; 
     } 
     else if (!categoryName.equals(other.categoryName)) 
     { 
      return false; 
     } 

     if (parentCategory == null) 
     { 
      if (other.parentCategory == null) 
      { 
       return true; 
      } 

      return false; 
     } 
     else 
     { 
      if (other.parentCategory != null) 
      { 
       return parentCategory.equals(other.parentCategory); 
      } 

      return false; 
     } 
    } 
} 

上述类代表一本书类别[编程技术等。每个类别都有一个父类别[可以为*类别为空]和几个子类别。

我添加了下面的代码来添加一个父类别,然后是一个子类别,然后尝试删除子类别。

@Test 
    public void testAddCategory() 
    { 
     try 
     { 
      HibernateUtilities.init(); 

      Category parent = new Category("category 4", " description 4");   
      Category child = new Category("child 12", "desc 12"); 

      //assume valid session object 
      session.beginTransaction(); 
      session.save(category); 
      session.getTransaction().commit(); 

      child.setParentCategory(parent); 
      parent.addSubCategory(child); 

      session.beginTransaction(); 
      session.save(category); 
      session.getTransaction().commit(); 

      session.beginTransaction(); 
      session.delete(category); 
      session.getTransaction().commit(); 
     } 
     catch (HibernateException e) 
     { 
      System.out.println(e); 
     } 
    } 

现在的问题是,删除我的子对象时,我的父类别对象也被删除。

我在哪里可以忽略这一点?

非常感谢。

得到@ManyToOne摆脱cascade的。

如果你想获得父母的缺失已删除的子对象,添加cascade=CascadeType.ALL@OneToMany

+0

感谢。解决了它。现在,如果我希望在删除父母时删除我的子对象,我该怎么办? –

+1

您需要指定@OneToMany(orphanRemoval = true) –

您已经将类型ALL级联设置为与父级关联。所以,当然,对孩子级联到父删除操作:

@ManyToOne(cascade = CascadeType.ALL, fetch=FetchType.EAGER) 
private Category parentCategory;