休眠多对多正确删除
Hibernate many to many proper delete
我正在尝试使用休眠和 j2ee 创建一个非常非常虚拟的字典后端。 (就是为了学习这些技术。)
我有两个实体 'Word' 和 'Category',它们之间存在多对多关系。我想达到的是,如果我删除一个类别,那么它会从所有受影响的词的类别中删除,但这些词仍然存在,如果我删除一个词,那么从类别的角度来看什么都不会发生。即使该类别中没有更多单词,该类别也应该存在。
目前我无法删除至少有一个词的类别。 (接收:org.hibernate.exception.ConstraintViolationException:无法执行语句)
当我删除一个词时,如果该类别中没有更多词,那么该类别也将与该词一起删除。
这是我声明实体的方式:
@Entity
@Table(name = "Word")
public class Word {
@Id()
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String uid;
@NotNull
@Column(nullable = false, length = 512)
private String hungarian;
@NotNull
@Column(nullable = false, length = 512)
private String english;
@JoinColumn(name = "categories_uid")
@ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
@Valid
private Set<Category> categories;
...
}
@Entity
@Table(name = "Category")
public class Category {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String uid;
@NotNull
@Size(min = 3, max = 512)
@Column(nullable = false, length = 512)
private String name;
...
}
我没有在类别 class 中创建集合,因为该集合中会有很多词,这可能会导致性能问题。
如果可能的话,我不会在类别 class.
中创建集合
我认为 CascadeType.ALL 应该处理这个问题。
提前致谢。
由于外键约束,您的删除操作失败。
在这种情况下,您可以在多对多关系中定义一个所有者端,即 Word,然后当您删除一个 Word 时,该关系将自动处理。但是如果你想删除一个类别,你必须像这样手动删除它:
@Entity
public class Word {
@ManyToMany
Set<Category> categories;
@Entity
public class Category {
@ManyToMany(mappedBy="categories")
Set<Word> words;
// call you entity manager to remove a category
em.remove(category);
for (Word word : category.words) {
word.categories.remove(category);
}
我正在尝试使用休眠和 j2ee 创建一个非常非常虚拟的字典后端。 (就是为了学习这些技术。)
我有两个实体 'Word' 和 'Category',它们之间存在多对多关系。我想达到的是,如果我删除一个类别,那么它会从所有受影响的词的类别中删除,但这些词仍然存在,如果我删除一个词,那么从类别的角度来看什么都不会发生。即使该类别中没有更多单词,该类别也应该存在。
目前我无法删除至少有一个词的类别。 (接收:org.hibernate.exception.ConstraintViolationException:无法执行语句) 当我删除一个词时,如果该类别中没有更多词,那么该类别也将与该词一起删除。
这是我声明实体的方式:
@Entity
@Table(name = "Word")
public class Word {
@Id()
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String uid;
@NotNull
@Column(nullable = false, length = 512)
private String hungarian;
@NotNull
@Column(nullable = false, length = 512)
private String english;
@JoinColumn(name = "categories_uid")
@ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
@Valid
private Set<Category> categories;
...
}
@Entity
@Table(name = "Category")
public class Category {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String uid;
@NotNull
@Size(min = 3, max = 512)
@Column(nullable = false, length = 512)
private String name;
...
}
我没有在类别 class 中创建集合,因为该集合中会有很多词,这可能会导致性能问题。 如果可能的话,我不会在类别 class.
中创建集合我认为 CascadeType.ALL 应该处理这个问题。
提前致谢。
由于外键约束,您的删除操作失败。 在这种情况下,您可以在多对多关系中定义一个所有者端,即 Word,然后当您删除一个 Word 时,该关系将自动处理。但是如果你想删除一个类别,你必须像这样手动删除它:
@Entity
public class Word {
@ManyToMany
Set<Category> categories;
@Entity
public class Category {
@ManyToMany(mappedBy="categories")
Set<Word> words;
// call you entity manager to remove a category
em.remove(category);
for (Word word : category.words) {
word.categories.remove(category);
}