【问题标题】:Hibernate inserts duplicates into one-to-many collection - eager fetchHibernate 将重复项插入到一对多集合中 - 急切获取
【发布时间】:2016-06-28 18:12:15
【问题描述】:

我有两个类,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

【问题讨论】:

    标签: java hibernate


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2011-12-15
      • 2011-06-24
      • 2010-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多