我有三个实体。两者ResolvedCorpus
和UnqualifiedCorpus
都是Corpus
帮助建模和解决某些问题的子类Corpus
。
通过 JPA 为数据持久性选择联合继承类型。实体定义如下。
@Entity
@Table(name="corpus")
@Inheritance(strategy = InheritanceType.JOINED)
public class Corpus implements Serializable {}
@Entity
@Table(name = "resolved_corpus")
@OnDelete(action=OnDeleteAction.CASCADE)
public class ResolvedCorpus extends Corpus {}
@Entity
@Table(name="unqualified_corpus")
@OnDelete(action=OnDeleteAction.NO_ACTION)
public class UnqualifiedCorpus extends Corpus {}
指定注释后,@OnDelete
我想实现以下目标:
- 当
ResolvedCorpus
通过 删除 a 时repository.delete()
,对应Corpus
的也会在 Table 中删除corpus
; - 当 a
UnqualifiedCorpus
通过 被删除时repository.delete()
,对应Corpus
的将保留。
但是,@OnDelete
不能按预期工作。顺便说一句,H2 用作嵌入式数据库。
测试代码复制如下。
//create corpora, one for each type: normal, resolved, unqualified.
Corpus normal = new Corpus();
normal.setName("normal");
corpusRepository.save(normal);
ResolvedCorpus resolved = new ResolvedCorpus();
resolved.setName("resolved");
resolved.setResolved("resolved");
resolvedCorpusRepository.save(resolved);
UnqualifiedCorpus unqualified = new UnqualifiedCorpus();
unqualified.setName("unqualified");
unqualified.setReason("unqualified");
unqualifiedCorpusRepository.save(unqualified);
//query
// 3 records are expected in corpus table
// 1 record in resolved table
// 1 in unqualified table
assertTrue(corpusRepository.count() == 3);
assertTrue(resolvedCorpusRepository.count() == 1);
assertTrue(unqualifiedCorpusRepository.count() == 1);
resolvedCorpusRepository.findAll().forEach((item) -> {
assertTrue(item.getName().equals("resolved"));
});
unqualifiedCorpusRepository.findAll().forEach((item) -> {
assertTrue(item.getName().equals("unqualified"));
});
//delete
// when deleting resolved, the delete action is expected to get cascaded to ancestor table "corpus", as OnDeleteAction suggested.
// when deleting unqualified, the delete action is not expected to get cascaded to ancestor table "corpus", as OnDeleteAction suggested.
resolvedCorpusRepository.deleteAll();
unqualifiedCorpusRepository.deleteAll();
//query
//HOWEVER, OnDeleteAction.NO_ACTION is NOT working as expected, and this assertion is failed.
assertTrue(corpusRepository.count() == 2);
您可以在此处找到测试应用程序。
我诊断了用于表创建和行删除的 DDL。我怀疑它@OnDelete
的目的是相反的,这意味着它控制子实体在基础实体对象被删除时的行为方式。调用时repository.delete(Resolved)
,生成的 DDLdelete from corpus
不是delete from resolved_corpus
; 和 for Unqualified
,delete from unqualified
和delete from corpus
都按顺序调用。
我想,像我这样的请求应该是 JPA 期望的合理功能,但是在网上搜索后我找不到合适的解决方案。