java - Hibernate:删除子对象

标签 java hibernate

我有以下 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

关于java - Hibernate:删除子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8200050/

相关文章:

java - Selenium DoubleClick WebElement 导致错误

java - 在自定义 validator Spring Rest 中返回 HTTP 代码

java - Spring Maven 项目

java - 使用 JPA 和 Spring 加载选定的 Hibernate 实体

java - iTextpdf 背景仅设置为可用文本

java - Java中HashMap的排序

java - Tomcat 自定义类加载器加载某些 jar 失败

hibernate - jpa 中 id 的一对一损坏列映射?

java - 将 java.util.List 转换为 ResultSet?

java - 从数据库流式传输数据集