-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate persisting duplicates from one-to-many collection
PostPosted: Tue Jun 28, 2016 2:25 pm 
Newbie

Joined: Mon Oct 19, 2015 2:38 pm
Posts: 4
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 (http://stackoverflow.com/questions/7903 ... collection); however, in this case, we are not using a lazy collection. Here is the relevant code:

MonitoringExpression.Class

Code:
@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

Code:
@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


Top
 Profile  
 
 Post subject: Re: Hibernate persisting duplicates from one-to-many collection
PostPosted: Wed Jun 29, 2016 2:11 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You need to make sure that both sides of the associations are synchronised.

So, MonitoringExpression should have two methods:

- addReportTrigger
- removeReposrtTrigger

These methods will add/remove the ReportTrigger from the one-to-many collection while also setting the many-to-one side as well


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.