Java JPA hibernate中一对一关系中的外键约束子级

Java JPA hibernate中一对一关系中的外键约束子级,java,spring,hibernate,jpa,spring-boot,Java,Spring,Hibernate,Jpa,Spring Boot,我将spring boot与JPA一起使用,我有一个实体,其中包含与下面描述的子实体相同的实体how nextCondition from type RuleCondition: @Entity @Table(name = "EDITOR_REGRA_CONDICAO") public class RuleCondition implements Serializable { @GenericGenerator( name = "ruleConditionSequenceGe

我将spring boot与JPA一起使用,我有一个实体,其中包含与下面描述的子实体相同的实体how nextCondition from type RuleCondition:

@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_索引_1 ON PUBLIC.EDITOR_REGRA_condico(字段)值(1,1)”;SQL语句: 在编辑器中插入条件(字段、下一个条件、连接器、运算符、值、id)值(?,,,,,,?,,?)[23505-193]


从上下文中,似乎插入的
RuleCondition
链接到了一个
字段
,该字段已经链接了一个
RuleCondition

一对一
关系中,这是不允许的(如名称所示,此类关系仅限于单个关系)

也要解决这个问题

对字段使用
多对一
关系:

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

或者确保在每次保存之前,没有指向已链接RuleCondition的字段的链接。

您插入的字段似乎带有已存在的字段ID。。有了唯一的约束,你就得到了错误,有没有可能在出现错误的时候,你已经有了相同字段的RuleCondition?由于这是一对一的关系,您不能拥有多个关系。谢谢@Ovidu Dolha,我将我的对象的关系修改为多个关系