休眠在使用 Cascade All 批量插入父实体期间显示非唯一对象异常.尝试了所有的可能性



我知道这个问题在这个论坛上被问了很多次,但我相信有时会出现新的问题。这可能是一种类型。所以我有一个父子实体的这段代码。父项与子项之间的关系是通过某个列连接的一对多关系。级联类型是 ALL(也尝试过其他人)。生成策略是序列,这意味着Hibernate在插入时分配它。 现在,随着插入多个id为null的记录,我得到了这个NonUniqueObjectException,会话抱怨另一个标识符。我尝试了很多尝试,但没有一个达到孩子入的程度。如果级联被完全删除,则进程运行良好,但实际上没有插入子级。这是我的实体以及我尝试插入的记录。我需要什么: 1.如何让休眠知道它是一个不同的对象(在我的情况下,休眠不知何故没有使用等于方法,我不知道休眠在哪里使用它tbh。由于我的第二列具有不同的值,因此它们确实是不同的对象,即哈希码。 2.以后如何插入子项,但更新父项中的引用。

父实体

@Entity
@Table(name = "PARENT_ENTITY")
public class ParentEntity implements Serializable {
private static final long serialVersionUID = 6072072740006570718L;
@Id
@SequenceGenerator(name = "ResultSeqPE", sequenceName = "SEQ_PARENT_ENTITY", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ResultSeqPE")
private Integer id;

@Column(name = "PE_DATE", nullable = false, updatable = false)
private LocalDate date;
@Column(name = "COL1", nullable = false, updatable = false)
private Double ColumnOne;
@Column(name = "COL2", updatable = false)
private Double ColumnTwo;
@Column(name = "COL3", updatable = false)
private Double ColumnThree;
@OneToMany(mappedBy = "childEntityCol", fetch = FetchType.EAGER, cascade=CascadeType.ALL)
private List<ChildEntity> childEntyValues = new ArrayList<>();
@Transient
private final int numOfFactors=5;
@Column(name = "COL4", nullable = true)
private String ColumnFour;
public String getColumnFour() {
return ColumnFour;
}
public void setColumnFour(String ColumnFour) {
this.ColumnFour = ColumnFour;
}
public ParentEntity(){}
public ParentEntity(LocalDate date, Double ColumnThree, String ColumnFour){
this( date, ColumnThree, null, null,ColumnFour);
}
public ParentEntity( LocalDate date, Double ColumnThree, Double ColumnTwo, Double ColumnOne, String ColumnFour){
this.date = date;
this.ColumnThree = ColumnThree;
this.ColumnTwo = ColumnTwo;
this.ColumnOne = ColumnOne;
this.ColumnFour=ColumnFour;
}

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}


public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}

public List<ChildEntity> getChildEntyValues() {
return childEntyValues;
}
public void setChildEntyValues(List<ChildEntity> childEntyValues) {
this.childEntyValues = childEntyValues;
}

public Double getColumnOne() {
return ColumnOne;
}
public void setColumnOne(Double ColumnOne) {
this.ColumnOne = ColumnOne;
}

public Double getColumnTwo() {
return ColumnTwo;
}
public void setColumnTwo(Double ColumnTwo) {
this.ColumnTwo = ColumnTwo;
}

public Double getColumnThree() {
return ColumnThree;
}
public void setColumnThree(Double ColumnThree) {
this.ColumnThree = ColumnThree;
}

@Override
public String toString() {
return "ParentEntity{" +
"id=" + id +
", date=" + date +
", ColumnOne=" + ColumnOne +
", ColumnTwo=" + ColumnTwo +
", ColumnThree=" + ColumnThree +
",ColumnFour=" + ColumnFour+
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ParentEntity)) return false;
ParentEntity that = (ParentEntity) o;
return new EntityEqualsBuilder()
.append(numOfFactors, that.numOfFactors)
.append(ColumnFour, that.ColumnFour)
.append(getDate(), that.getDate())
.append(getColumnOne(), that.getColumnOne())
.append(getColumnTwo(), that.getColumnTwo())
.append(ColumnThree, that.ColumnThree)
.isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(getDate())
.append(getColumnOne())
.append(getColumnTwo())
.append(numOfFactors)
.append(ColumnFour)
.toHashCode();
}
}

子实体

import org.apache.commons.lang3.builder.HashCodeBuilder;
import java.io.Serializable;
import javax.persistence.*;
import javax.validation.constraints.Size;
@Entity
@Table( name = "CHILD_ENTITY")
public class ChildEntity implements Serializable {

/**
* 
*/
private static final long serialVersionUID = 8229169287136307307L;

@Id
@SequenceGenerator(name = "ResultValueSeqCHLD", sequenceName = "SEQ_CHILD_ENTITY", allocationSize = 20)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ResultValueSeqCHLD")
private Integer id;
@Column(name = "COL1")
private String ChildColumnOne;
@Size(max = 100)
@Column(name = "COL2", nullable = false, updatable = false)
private String ChildColumnTwo;
@Column(name = "COL3", nullable = false, updatable = false)
private Double ChildColumnThree;
//Standard deviation
@Column(name = "COL4", nullable = true)
private Double ChildColumnFour;
@Column(name = "COL5", nullable = true)
private String ChildColumnFive;

public String getChildColumnFive() {
return ChildColumnFive;
}
public void setChildColumnFive(String ChildColumnFive) {
this.ChildColumnFive = ChildColumnFive;
}
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "RESULT_ID", nullable = true, updatable = true)
private ParentEntity childEntityCol;

@Transient
private static final int numOfFactors=5;
public ChildEntity() {
}
public ChildEntity(ParentEntity childEntityCol,
String ChildColumnOne,
String ChildColumnTwo,
Double ChildColumnThree,
Double ChildColumnFour,
String rmvdfctr) {
this.childEntityCol = childEntityCol;
this.ChildColumnOne = ChildColumnOne;
this.ChildColumnTwo = ChildColumnTwo;
this.ChildColumnThree = ChildColumnThree;
this.ChildColumnFour = ChildColumnFour;
this.ChildColumnFive=rmvdfctr;
}
public Integer getId(){ return id; }
public void setId(Integer id){
this.id = id;
}
public Double getChildColumnThree() {
return ChildColumnThree;
}
public void setChildColumnThree(Double ChildColumnThree) {
this.ChildColumnThree = ChildColumnThree;
}
public String getChildColumnOne() {
return ChildColumnOne;
}
public void setChildColumnOne(String ChildColumnOne) {
this.ChildColumnOne = ChildColumnOne;
}
public String getChildColumnTwo() {
return ChildColumnTwo;
}
public void setChildColumnTwo(String ChildColumnTwo) {
this.ChildColumnTwo = ChildColumnTwo;
}
public ParentEntity getchildEntityCol() {
return childEntityCol;
}
public void setchildEntityCol(ParentEntity childEntityCol) {
this.childEntityCol = childEntityCol;
}
public Double getChildColumnFour() {
return ChildColumnFour;
}
public void setChildColumnFour(Double ChildColumnFour) {
this.ChildColumnFour = ChildColumnFour;
}
@Override
public String toString() {
return "CrossSectionResultValueFiveFactors{" +
"id=" + id +
", ChildColumnOne=" + ChildColumnOne +
", ChildColumnTwo='" + ChildColumnTwo + ''' +
", ChildColumnThree=" + ChildColumnThree +
", ChildColumnFour=" + ChildColumnFour +
", ChildColumnFive=" + ChildColumnFive +
'}';
}
@Override
public boolean equals(Object o) {
System.out.println("Equals method called");
if (this == o) return true;
if (!(o instanceof ChildEntity)) return false;
ChildEntity that = (ChildEntity) o;
if(ChildColumnFive != that.ChildColumnFive){
return false;
}
return new EntityEqualsBuilder()
.append(ChildColumnFive, that.ChildColumnFive)
.append(getChildColumnOne(), that.getChildColumnOne())
.append(childEntityCol.getDate(), that.childEntityCol.getDate())
.append(numOfFactors, that.numOfFactors)
.append(getChildColumnTwo(), that.getChildColumnTwo())
.append(getChildColumnThree(), that.getChildColumnThree())
.append(getChildColumnFour(), that.getChildColumnFour())
.isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(numOfFactors)
.append(ChildColumnFive)
.append(getChildColumnOne())
.append(getChildColumnTwo())
.append(getChildColumnThree())
.append(getChildColumnFour())
.append(childEntityCol.getDate())
.toHashCode();
}
}

Hibernate尝试为子实体插入的记录

id=null,ChildColumnOne=ARTG,ChildColumnTwo=BB,ChildColumnThree=0.017343977423166613,ChildColumnFour=4.5005492463416223E-4,ChildColumnFive=TENOR
id=null,ChildColumnOne=ARTG,ChildColumnTwo=AA,ChildColumnThree=0.0,ChildColumnFour=null,ChildColumnFive=TENOR
id=null,ChildColumnOne=ARTG,ChildColumnTwo=AAA,ChildColumnThree=-0.0032299113437670047,ChildColumnFour=4.5125704017972683E-4,ChildColumnFive=TENOR
id=null,ChildColumnOne=ARTG,ChildColumnTwo=A,ChildColumnThree=0.00271525070429559,ChildColumnFour=2.68546862862496E-4,ChildColumnFive=TENOR
id=null,ChildColumnOne=ARTG,ChildColumnTwo=CCC,ChildColumnThree=0.06180573527799885,ChildColumnFour=0.0013944546117282551,ChildColumnFive=TENOR
id=null,ChildColumnOne=ARTG,ChildColumnTwo=B,ChildColumnThree=0.035266618793342724,ChildColumnFour=6.903395055330724E-4,ChildColumnFive=TENOR
id=null,ChildColumnOne=ARTG,ChildColumnTwo=BBB,ChildColumnThree=0.006277560784636706,ChildColumnFour=2.9452395925335794E-4,ChildColumnFive=TENOR
id=null,ChildColumnOne=SCTR,ChildColumnTwo=XX,ChildColumnThree=-7.484158323833947E-4,ChildColumnFour=3.209042959626849E-4,ChildColumnFive=TENOR
id=null,ChildColumnOne=SCTR,ChildColumnTwo=YY,ChildColumnThree=0.0010161770578058535,ChildColumnFour=5.450833322608508E-4,ChildColumnFive=TENOR
id=null,ChildColumnOne=SCTR,ChildColumnTwo=ZZ,ChildColumnThree=0.0012685617889754614,ChildColumnFour=4.2303152747200215E-4,ChildColumnFive=TENOR
id=null,ChildColumnOne=SCTR,ChildColumnTwo=PP,ChildColumnThree=-0.0015318036574709301,ChildColumnFour=5.326195888612156E-4,ChildColumnFive=TENOR
id=null,ChildColumnOne=SCTR,ChildColumnTwo=QQ,ChildColumnThree=-6.929110009865118E-4,ChildColumnFour=6.329592108245802E-4,ChildColumnFive=TENOR
id=null,ChildColumnOne=SCTR,ChildColumnTwo=RR,ChildColumnThree=-0.0019122958178337181,ChildColumnFour=3.4641562920409316E-4,ChildColumnFive=TENOR
id=null,ChildColumnOne=SCTR,ChildColumnTwo=SS,ChildColumnThree=-0.0019080966821294664,ChildColumnFour=3.887778671973025E-4,ChildColumnFive=TENOR
id=null,ChildColumnOne=SCTR,ChildColumnTwo=TT,ChildColumnThree=-0.0011255034583223175,ChildColumnFour=2.4576405278469896E-4,ChildColumnFive=TENOR
id=null,ChildColumnOne=SCTR,ChildColumnTwo=UU,ChildColumnThree=-4.083237085957168E-4,ChildColumnFour=3.239432062712418E-4,ChildColumnFive=TENOR
id=null,ChildColumnOne=SCTR,ChildColumnTwo=VV,ChildColumnThree=0.0,ChildColumnFour=null,ChildColumnFive=TENOR
id=null,ChildColumnOne=SCTR,ChildColumnTwo=WW,ChildColumnThree=-0.001826936857448209,ChildColumnFour=0.0012430200943232715,ChildColumnFive=TENOR
id=null,ChildColumnOne=CTRY,ChildColumnTwo=KK,ChildColumnThree=-0.001820836558888903,ChildColumnFour=6.075555350826326E-4,ChildColumnFive=TENOR
id=null,ChildColumnOne=CTRY,ChildColumnTwo=LL,ChildColumnThree=-0.0011289483934925119,ChildColumnFour=0.0011271116946331911,ChildColumnFive=TENOR
id=null,ChildColumnOne=CTRY,ChildColumnTwo=MM,ChildColumnThree=-0.006944573473560388,ChildColumnFour=4.4010977849485424E-4,ChildColumnFive=TENOR

会话抱怨的记录:

id=null,ChildColumnOne=CTRY,ChildColumnTwo=NN,ChildColumnThree=-0.007162728895175867,ChildColumnFour=4.7163949543625657E-4,ChildColumnFive=TENOR

在这里,我们看到不同的第二列。

一些限制: 我只能使用序列作为生成器。

请指教。我关注了弗拉德的博客和论坛上的许多其他博客,但没有成功。就我而言,是记录本身在根据会话抱怨。

例外情况:

org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [com.ing.ipa.dmd.bondproxy.core.model.CrossSectionResultValueFiveFactors#234]
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:165)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:678)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:670)

Hibernate的意思是你有两个对象,它们具有相同的主标识符,但它们不是同一个对象。

它最常通过级联保存发生,其中父实体和子实体之间存在级联保存,但子实体已与会话关联,但不在子实体的同一实例上。

相关内容

  • 没有找到相关文章

最新更新