JPA persist 在级联子级时插入不正确的 id 值
JPA persist is inserting incorrect id value when cascading a child
此代码失败
我在数据库中有一个现有的版本值,我使用 entity manager.find
获取它并在实体上设置。
VersionEntity version = entityManager.find(VersionEntity.class, 1);
entities.forEach(__ -> __.setVersion(version));
entities.forEach(entityManager::persist);
entityManager.flush();
我专门查找并分配了版本 1,那么它为什么还要尝试使用版本 2。我什至尝试了 merge
但没有用。
我的属性明显错误/不正确。
刷新失败
ocal Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.1.qualifier): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: insert or update on table "t_quality_measurement" violates foreign key constraint "fk_t_quality_measurement_version"
Detail: Key (version)=(2) is not present in table "t_version".
Error Code: 0
Call: INSERT INTO T_QUALITY_MEASUREMENT (BRAND, DATE, KPICODE, KPICOMPAREVW, KPICOMPAREWT, KPIVALUE, ORGANISATIONUNIT, PHASE, PLANT, SERIES, SIGNALCOLOR, TECHNOLOGY, TEMPORALUNIT, VIEW) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [14 parameters bound]
Query:
我不确定为什么它在我专门查找 1 时尝试使用 2。
@Entity
@Table(name = "T_QUALITY_MEASUREMENT")
public class QualityMeasurementEntity extends Versioned implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(columnDefinition = "serial")
private Integer id;
@Column(length = 100)
private String series;
版本化看起来像这样
@MappedSuperclass
public abstract class Versioned {
@ManyToOne
@JoinColumn(name = "version", insertable = false, updatable = false)
protected VersionEntity version = null;
public VersionEntity getVersion() { return version; }
public void setVersion(VersionEntity version) { this.version = version; }
}
和版本实体。
@Entity
@Table(name = "T_VERSION", indexes = {
@Index(name = "pk_version", columnList = "id", unique = true)
})
@NamedQuery(name = "Version.findById", query = "SELECT v FROM VersionEntity v WHERE v.id = :id")
public class VersionEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(columnDefinition = "serial")
private Integer id;
@Column(name = "CREATED")
private LocalDateTime created;
@Column
private Boolean active = false;
@PrePersist
protected void onCreate() {
created = LocalDateTime.now();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public LocalDateTime getCreated() { return created; }
public void setCreated(LocalDateTime created) { this.created = created; }
public Boolean getActive() { return active; }
public void setActive(Boolean active) { this.active = active; }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
VersionEntity version = (VersionEntity) o;
return id.equals(version.id);
}
@Override public int hashCode() { return id.hashCode(); }
}
不需要阻止插入。
更改 @JoinColumn
@JoinColumn(name = "version")
此代码失败
我在数据库中有一个现有的版本值,我使用 entity manager.find
获取它并在实体上设置。
VersionEntity version = entityManager.find(VersionEntity.class, 1);
entities.forEach(__ -> __.setVersion(version));
entities.forEach(entityManager::persist);
entityManager.flush();
我专门查找并分配了版本 1,那么它为什么还要尝试使用版本 2。我什至尝试了 merge
但没有用。
我的属性明显错误/不正确。
刷新失败
ocal Exception Stack: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.1.qualifier): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: org.postgresql.util.PSQLException: ERROR: insert or update on table "t_quality_measurement" violates foreign key constraint "fk_t_quality_measurement_version" Detail: Key (version)=(2) is not present in table "t_version". Error Code: 0 Call: INSERT INTO T_QUALITY_MEASUREMENT (BRAND, DATE, KPICODE, KPICOMPAREVW, KPICOMPAREWT, KPIVALUE, ORGANISATIONUNIT, PHASE, PLANT, SERIES, SIGNALCOLOR, TECHNOLOGY, TEMPORALUNIT, VIEW) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) bind => [14 parameters bound] Query:
我不确定为什么它在我专门查找 1 时尝试使用 2。
@Entity
@Table(name = "T_QUALITY_MEASUREMENT")
public class QualityMeasurementEntity extends Versioned implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(columnDefinition = "serial")
private Integer id;
@Column(length = 100)
private String series;
版本化看起来像这样
@MappedSuperclass
public abstract class Versioned {
@ManyToOne
@JoinColumn(name = "version", insertable = false, updatable = false)
protected VersionEntity version = null;
public VersionEntity getVersion() { return version; }
public void setVersion(VersionEntity version) { this.version = version; }
}
和版本实体。
@Entity
@Table(name = "T_VERSION", indexes = {
@Index(name = "pk_version", columnList = "id", unique = true)
})
@NamedQuery(name = "Version.findById", query = "SELECT v FROM VersionEntity v WHERE v.id = :id")
public class VersionEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(columnDefinition = "serial")
private Integer id;
@Column(name = "CREATED")
private LocalDateTime created;
@Column
private Boolean active = false;
@PrePersist
protected void onCreate() {
created = LocalDateTime.now();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public LocalDateTime getCreated() { return created; }
public void setCreated(LocalDateTime created) { this.created = created; }
public Boolean getActive() { return active; }
public void setActive(Boolean active) { this.active = active; }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
VersionEntity version = (VersionEntity) o;
return id.equals(version.id);
}
@Override public int hashCode() { return id.hashCode(); }
}
不需要阻止插入。
更改 @JoinColumn
@JoinColumn(name = "version")