简体   繁体   中英

Hibernate inserts duplicates into one-to-many collection - eager fetch

I have two classes, monitoringExpression and reportTrigger, with a one-to-many relationship. Hibernate is attempting to persist duplicate reportTriggers from the collection held by the monitoringExpression class. The first insert into the reportTrigger collection works, but subsequent inserts fail with a unique constraint violation because hibernate tries to persist the same reportTrigger twice. This is quite similar to a known hibernate bug ( Hibernate inserts duplicates into a @OneToMany collection ); however, in this case, we are not using a lazy collection. Here is the relevant code:

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;
}

As far as I can tell, we're not doing anything out of the ordinary to the reportTrigger collection (and we obviously cannot be adding the same tigger twice to a set). Has anyone seen anything like this? Thanks

Hibernate 3.6.10

Java 8

I think can do some checking as below
- Check unique constraint violation error you got is on which field ? It can be on Primary key column but also can any unique columns.
- If the violation is with your primary key then while your collection is a set then it can be 2 different TriggerableReport but share the same key value.

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