gpt4 book ai didi

java - Hibernate:删除子对象

转载 作者:行者123 更新时间:2023-12-02 05:32:14 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com