我遇到了一个非常烦人的问题,即在对象合并到EntityManager后,实体失去了状态。
在该应用程序中,有一个ExpeditureStatements的“档案”,其中包含许多支出。
这些支出可以(部分)由多个债务人索偿。
将为ExpenditureStatement创建一个ExpenditureStatementClaim。
在ExpenditureStatement上为每个支出创建一个ExpenditureClaim。
ExpenditureClaims和ExpenditureStatementClaim都持久存在,没有任何问题。
但是,在entitymanager上的合并被称为后,支出将失去其状态:
em.merge(档案)。
但是,每笔支出中的数据都会还原到数据库中的最后状态。
我已经尝试过仅从上到下级联,我对equals / hashcode进行了更改,但这没有任何改变。
是否有人对导致此问题的原因有任何线索?
卷宗:
@Entity
@Table(name = "DOSSIER")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DOSSIER_TYPE", discriminatorType = DiscriminatorType.STRING, length = 48)
public abstract class Dossier {
@OneToMany(mappedBy = ExpenditureStatement.PROP_DOSSIER, cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private List<ExpenditureStatement> expenditureStatements = new ArrayList<ExpenditureStatement>();
}
支出报表:
@Entity
@Table(name = "EXPENDITURE_STATEMENT")
public class ExpenditureStatement {
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE})
@JoinColumn(name = "DOSSIER_ID", nullable = false)
private Dossier dossier;
@OneToMany(mappedBy = Expenditure.PROP_EXPENDITURE_STATEMENT, cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private List<Expenditure> expenditures = new ArrayList<Expenditure>();
@OneToMany(mappedBy = ExpenditureStatementClaim.PROP_EXPENDITURE_STATEMENT, cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private Collection<ExpenditureStatementClaim> expenditureStatementClaims = new ArrayList<ExpenditureStatementClaim>();
}
支出:
@Entity
@Table(name = "EXPENDITURE")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "EXPENDITURE_ORIGIN_CD", discriminatorType = DiscriminatorType.STRING, length = 48)
public abstract class Expenditure extends EntityObject<Long> {
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "EXPENDITURE_STATEMENT_ID", nullable = false)
private ExpenditureStatement expenditureStatement;
@OneToMany(mappedBy = ExpenditureClaim.PROP_EXPENDITURE, cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private Collection<ExpenditureClaim> claims = new HashSet<>();
@NotNull
@Column(name = "EXPENDITURE_STATUS_CD", nullable = false)
@Enumerated(EnumType.STRING)
private ExpenditureStatus status;
public abstract BigDecimal getAmount();
}
ExpenditureStatement声明:
@Entity
@Table(name = "DEEL_STAAT")
public class ExpenditureStatementClaim {
@NotNull
@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name = "EXPENDITURE_STATEMENT_ID", nullable = false)
private ExpenditureStatement expenditureStatement;
@OneToMany(mappedBy = ExpenditureClaim.PROP_EXPENDITURE_STATEMENT_CLAIM, cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private Collection<ExpenditureClaim> expenditureClaims = new ArrayList<>();
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "INVOICE_ID")
private Invoice invoice;
}
支出说明:
@Entity
@Table(name = "EXPENDITURE_CLAIM")
public class ExpenditureClaim extends EntityObject<Long> {
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "EXPENDITURE_ID", nullable = false)
private Expenditure expenditure;
@Column(name = "AMOUNT", precision = NumberConstants.CURRENCY_PRECISION, scale = NumberConstants.CURRENCY_OPERATION_SCALE)
private BigDecimal amount;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "EXPENDITURE_ST_CLAIM_ID", nullable = false)
private ExpenditureStatementClaim expenditureStatementClaim;
}
最佳答案
该问题不是由映射问题引起的,而是由于(正在使用的)(相当旧的)EclipseLink版本中的错误,该项目正在使用该项目,因此新创建的对象将不会级联对现有对象的更改。
https://bugs.eclipse.org/bugs/show_bug.cgi?id=340802
提醒您,只要有可能,您都应尝试更新项目中依赖项的版本...