我有 4 张桌子:
RuleEntity 与 Dimension、Barcode 具有 OneToOne 关系,与 Keyword 具有 OneToMany 关系:
@OneToOne(mappedBy = "rule", cascade = CascadeType.ALL)
private RuleDimension ruleDimension;
@OneToOne(mappedBy = "rule", cascade = CascadeType.ALL)
private RuleBarcode ruleBarcode;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinColumn(name = "id_rule_", nullable = true)
private List<RuleKeyword> ruleKeywords = new ArrayList<RuleKeyword>();
Rule
表还有一个列 type
。这用于了解规则是使用维度对象、条形码还是关键字列表。
Dimension 和 Barcode 实体具有如下映射:
@OneToOne
@JoinColumn(name = "id_rule_")
private Rule rule;
虽然 Keyword
实体没有映射,因为我不想在这里进行双向映射,但我只需要对 Dimension
和 Barcode
进行双向映射。
我想要做的是,当我选择说类型 Dimension
时,我想删除此 Barcode
的数据库中的 Keyword
和 Rule
行.所以想法是Rule
只能有一个类型且选择类型时,应删除其他类型。
在我的 java 代码中,我是这样尝试的:
if (rule.getType().equals("dimension") {
rule.setRuleBarcode(null);
rule.setRuleKeywords(null);
} else if (rule.getType().equals("barcode") {
rule.setRuleDimension(null);
rule.setRuleKeywords(null);
} else if (rule.getType().equals("keywords") {
rule.setRuleBarcode(null);
rule.setRuleDimension(null);
}
但这没有帮助,我仍然看到数据库上的行。我也尝试这样做:
...
rule.getRuleBarcode().setRule(null);
...
对于其他类型也是如此,但结果仍然相同。我能做什么?
谢谢