JPA hibernate

时间:2017-05-03 12:37:30

标签: java spring hibernate jpa spring-boot

好吧,我正在使用带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)VALUES(1,1)&#34 ;; SQL语句:   插入editor_regra_condicao(字段,next_condition,连接符,运算符,值,id)值(?,?,?,?,?,?)[23505-193]

1 个答案:

答案 0 :(得分:0)

从上下文中可以看出,正在插入的RuleCondition链接到已与Field相关联的RuleCondition

关于one-to-one关系,这是不允许的(这种关系仅限于单一关系,顾名思义)

要解决此问题

对字段使用many-to-one关系:

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

或者确保在每次保存之前没有链接到已经链接了RuleCondition的Field的链接。