简体   繁体   English

JPA休眠中一对一关系中的外键约束子级

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

Well, I'm using the spring boot with JPA and I have an entity that contains the same entity as child described below how nextCondition from type RuleCondition: 好吧,我在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;

This is the rule condition controller 这是规则条件控制器

@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);
}

And finalizing this is the class responsible for managing rule condition operations: 最后确定的是负责管理规则条件操作的类:

 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);

}

When I am persisting the data, I am encountering the following error: 当我保留数据时,遇到以下错误:

Unique index or primary key violation: "UK_QG4N8FT2CPEX15N36TM2SRXPN_INDEX_1 ON PUBLIC.EDITOR_REGRA_CONDICAO(FIELD) VALUES (1, 1)"; 唯一索引或主键冲突:“ UK_QG4N8FT2CPEX15N36TM2SRXPN_INDEX_1 ON PUBLIC.EDITOR_REGRA_CONDICAO(FIELD)值(1、1)”; SQL statement: insert into editor_regra_condicao (field, next_condition, connector, operator, value, id) values (?, ?, ?, ?, ?, ?) [23505-193] SQL语句:插入到editor_regra_condicao(字段,next_condition,连接器,运算符,值,id)值(?,?,?,?,?,?)[23505-193]

From the context, it appears that the RuleCondition being inserted is linked to a Field that already has a RuleCondition linked to it. 从上下文,似乎在RuleCondition插入链接到Field已经有一个RuleCondition链接到它。

On one-to-one relationships, this is not allowed (such relations are restricted to single relations as the name suggests) one-to-one关系中,这是不允许的(顾名思义,此类关系仅限于单个关系)

To fix this either 要解决这个问题

Use a many-to-one relationship for Field: 对字段使用many-to-one关系:

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

OR make sure that you do not have a link to a Field that already has a RuleCondition linked to it before each save. 或者,在每次保存之前,请确保没有链接到已经有RuleCondition链接到的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM