简体   繁体   中英

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:

@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)"; SQL statement: insert into editor_regra_condicao (field, next_condition, connector, operator, value, id) values (?, ?, ?, ?, ?, ?) [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.

On one-to-one relationships, this is not allowed (such relations are restricted to single relations as the name suggests)

To fix this either

Use a many-to-one relationship for Field:

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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