簡體   English   中英

JPA休眠中一對一關系中的外鍵約束子級

[英]foreign key constraint child in One To One relationship in JPA hibernate

好吧,我在JPA上使用了Spring Boot,並且我有一個實體,該實體包含與下面描述的子代相同的實體,下面介紹如何使用RuleCondition類型的nextCondition:

@Entity @Table(name = "EDITOR_REGRA_CONDICAO") 
public class RuleCondition implements Serializable {

@GenericGenerator(
        name = "ruleConditionSequenceGenerator",
        strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
        parameters = {
                @Parameter(name = "sequence_name", value = "SEQ_RULE_CONDITION"),
                @Parameter(name = "initial_value", value = "1"),
                @Parameter(name = "increment_size", value = "1")
        })
@GeneratedValue(generator = "ruleConditionSequenceGenerator")
@Id
private Long id;

@OneToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "field", nullable = false)
private Field field;

@Column
private String value;

@OneToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "operator", nullable = false)
private RuleOperator ruleOperator;

@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "connector")
private RuleConnector ruleConnector;

@OneToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
@JoinColumn(name = "next_condition")
private RuleCondition nextCondition;

這是規則條件控制器

@RequestMapping(value = "/rule", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public Rule newRule(@RequestParam("layout") Long layoutId, @RequestBody Rule rule) {
    return ruleManager.newRule(rule,layoutId);
}

最后確定的是負責管理規則條件操作的類:

 public Rule newRule(@Nonnull final Rule rule, @Nonnull final Long layoutId) {

    RuleType ruleType = ruleService.getRuleType(rule.getRuleType().getIdentifier());

    saveConditions(rule.getCondition());

    rule.setRuleType(ruleType);

    Rule savedRule = ruleService.saveRule(rule);

    layoutManager.addRule(savedRule, layoutId);

    return savedRule;
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
private void saveConditions(RuleCondition ruleCondition) {

    RuleConnector ruleConnector;
    RuleOperator ruleOperator;

    if (ruleCondition == null) {
        return;
    }

    if (ruleCondition.getNextCondition() != null) {
        saveConditions(ruleCondition.getNextCondition());
    }

    if (ruleCondition.getRuleConnector() != null) {
        ruleConnector = ruleService.getRuleConnector(ruleCondition.getRuleConnector().getIdentifier());
        ruleCondition.setRuleConnector(ruleConnector);
    }

    if (ruleCondition.getRuleOperator() != null) {
        ruleOperator = ruleService.getRuleOperator(ruleCondition.getRuleOperator().getIdentifier());
        ruleCondition.setRuleOperator(ruleOperator);
    }

    if (ruleCondition.getField() != null) {
        Field field = fieldManager.getFieldByName(ruleCondition.getField().getName());
        ruleCondition.setField(field);
    }

    ruleService.saveCondition(ruleCondition);

}

當我保留數據時,遇到以下錯誤:

唯一索引或主鍵沖突:“ UK_QG4N8FT2CPEX15N36TM2SRXPN_INDEX_1 ON PUBLIC.EDITOR_REGRA_CONDICAO(FIELD)值(1、1)”; SQL語句:插入到editor_regra_condicao(字段,next_condition,連接器,運算符,值,id)值(?,?,?,?,?,?)[23505-193]

從上下文,似乎在RuleCondition插入鏈接到Field已經有一個RuleCondition鏈接到它。

one-to-one關系中,這是不允許的(顧名思義,此類關系僅限於單個關系)

要解決這個問題

對字段使用many-to-one關系:

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "field", nullable = false)
private Field field;

或者,在每次保存之前,請確保沒有鏈接到已經有RuleCondition鏈接到的字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM