2016-06-28 15 views
0

我有兩個班,monitoringExpression和reportTrigger,用一個一對多的關係。 Hibernate試圖從monitoringExpression類持有的集合中保存重複的reportTriggers。第一次插入到reportTrigger集合中工作,但隨後的插入失敗並導致唯一的約束衝突,因爲hibernate會嘗試保留相同的reportTrigger兩次。這與已知的休眠錯誤(Hibernate inserts duplicates into a @OneToMany collection)非常相似;然而,在這種情況下,我們並沒有使用懶惰的集合。下面是相關代碼:休眠重複插入到一個一對多的集合 - 預先抓取

MonitoringExpression.Class

@Audited 
@Entity 
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
public class MonitoringExpression extends GeneratedIdXmlObject{ 

private String name; 
private DeterminantDefinition determinantDefinition; 
private String valueExpression; 
private String testExpression; 
private String messageExpression; 
private String messageSeverity; 
private boolean setExitStatusWhenTrue; 
protected SortedSet<MonitoringExpressionAttribute> attributes = new TreeSet<MonitoringExpressionAttribute>(); 
private String color; 

private Set<ReportTrigger> reportTriggers = new HashSet<ReportTrigger>();    

. 
. 
. 

@OneToMany(mappedBy="monitoringExpression",orphanRemoval=true,cascade={CascadeType.ALL},fetch=FetchType.EAGER) 
@Sort(type=SortType.NATURAL) 
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
public SortedSet<MonitoringExpressionAttribute> getAttributes() { 
    return attributes; 
} 
public void setAttributes(SortedSet<MonitoringExpressionAttribute> attributes) { 
    this.attributes = attributes; 
} 

ReportTrigger.Class

@Audited 
@Table(name="ReportTrigger") 
@Entity 
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
public class ReportTrigger extends GeneratedIdXmlObject{ 

private String name; 
private String description; 
private TriggerableReport report; 
private Frequency burstPeriodSize; 
private String periodStartExpression; 
private String periodEndExpression; 
private Set<ReportTriggerParameterMapping> parameterMappings = new HashSet<ReportTriggerParameterMapping>();  
private MonitoringExpression monitoringExpression; 

@XmlIDREF 
@ManyToOne 
@Audited 
@GraphProcessorOverride(process=false,recurse=false) 
@NaturalId(mutable=true) 
public TriggerableReport getReport() { 
    return report; 
} 
public void setReport(TriggerableReport report) { 
    this.report = report; 
} 

@Embedded 
public Frequency getBurstPeriodSize() { 
    return burstPeriodSize; 
} 
public void setBurstPeriodSize(Frequency burstPeriodSize) { 
    this.burstPeriodSize = burstPeriodSize; 
} 

@Audited 
@OneToMany(mappedBy="reportTrigger",orphanRemoval=true,cascade={CascadeType.ALL}) 
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
public Set<ReportTriggerParameterMapping> getParameterMappings() { 
    return parameterMappings; 
} 

. 
. 
. 

@XmlIDREF 
@ManyToOne 
@GraphProcessorOverride(process=false,recurse=false) 
@NaturalId(mutable=true) 
public MonitoringExpression getMonitoringExpression() { 
    return monitoringExpression; 
} 
public void setMonitoringExpression(MonitoringExpression monitoringExpression) { 
    this.monitoringExpression = monitoringExpression; 
} 

據我所知,我們什麼都沒有做出來的普通到reportTrigger集合(我們顯然不能將兩個相同的tigger添加到集合中)。有沒有人看過類似的東西?由於

休眠3.6.10

的Java 8

回答

0

我覺得可以做一些檢查如下
- 檢查違反唯一約束錯誤,你得到的是哪個領域?它可以在主鍵列上,但也可以是任何獨特的列。
- 如果違規與您的主鍵相關,那麼當您的收集是一個集合時,它可以是2個不同的TriggerableReport,但共享相同的鍵值。