多个onetoone关系休眠

fcg9iug3  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(142)

我有4张table:
规则

条形码
关键字
ruleentity与维度、条形码有一个one关系,与关键字有一个one关系:

@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 . 这用于了解规则是否使用维度对象、条形码或关键字列表。
维度和条形码实体具有如下Map:

@OneToOne
@JoinColumn(name = "id_rule_")
private Rule rule;

Keyword 实体没有Map,因为我不需要双向Map,我只需要双向Map Dimension 以及 Barcode .
我想做的是当我选择say type时 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);
...

其他类型也一样,但结果仍然相同。我能做什么?
谢谢您

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题