Hibernate:删除子对象

时间:2011-11-20 07:20:48

标签: 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);
        }
    }

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

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

谢谢你的到来。

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:4)

cascade中删除@ManyToOne

如果您想在父删除时删除子对象,请将cascade=CascadeType.ALL添加到@OneToMany