2017-05-03 30 views
0

好,我使用的是JPA春天引導和我有一個包含下面如何nextCondition從類型RuleCondition描述相同的實體作爲孩子的實體:外鍵約束孩子在一,在JPA的一個關係冬眠

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

} 

當我堅持的數據,我遇到了以下錯誤:

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]

+0

它似乎是你插入一個fieldid已經存在..並有獨特的約束seet你會得到錯誤 –

+0

是否有可能在錯誤的位置你已經有一個RuleCondition爲同一個字段?由於這是一對一的關係,因此您不允許有多個關係。 –

回答

0

從上下文,似乎正在插入的RuleCondition被鏈接到已鏈接到該RuleCondition一個Field

one-to-one關係,這是不允許的(顧名思義這種關係僅限於單個關係)

爲了解決這個問題要麼

使用棒球場many-to-one關係:

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

或者確保您沒有指向每個保存前已經有一個RuleCondition鏈接到的字段的鏈接。

+0

謝謝@Ovidu Dolha, 我修改了我的對象與manyToOne的關係 –