Hibernate将重复项插入到一对多集合中 - 渴望获取

时间:2016-06-28 18:12:15

标签: java hibernate

我有两个类,monitoringExpression和reportTrigger,具有一对多的关系。 Hibernate试图从monitoringExpression类持有的集合中保留重复的reportTriggers。 reportTrigger集合中的第一个插入工作正常,但后续插入失败并出现唯一约束违规,因为hibernate尝试将同一reportTrigger保留两次。这与已知的hibernate bug(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集合做任何不寻常的事情(我们显然不能将两次相同的跳跳器添加到一个集合中)。有没有人见过这样的东西?感谢

Hibernate 3.6.10

Java 8

1 个答案:

答案 0 :(得分:0)

我认为可以进行如下检查 - 检查您获得的唯一约束违规错误是在哪个字段?它可以在主键列上,但也可以是任何唯一列 - 如果违规是您的主键,那么当您的集合是一个集合时,它可以是2个不同的TriggerableReport但共享相同的键值。