Search code examples
javajpaeclipselinkpersistenceentitymanager

Entity loses state after calling merge on entitymanager


I'm running into a very annoying issue whereby an entity loses its state after the object is merged into the EntityManager.

In the application there's a "Dossier" with ExpenditureStatements which has a number of expenditures.

These expenditures can be (partially) claimed from multiple debtors. An ExpenditureStatementClaim is created for the ExpenditureStatement. An ExpenditureClaim is created for each Expenditure on the ExpenditureStatement.

Both the ExpenditureClaims and the ExpenditureStatementClaim are persisted without any issues. The expenditures however lose their state after the merge on the entitymanager is called: em.merge(dossier).

However, the data in each expenditure reverts back to its last state in the database.

I've already tried cascading only top down, i've made changes to equals/hashcode but this didn't change anything.

Does anyone have a clue as to what might be causing this issue?

Dossier:

@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>();
}

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>();
}

Expenditure:

@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();
}

ExpenditureStatementClaim:

@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;
}

ExpenditureClaim:

@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;
}

Solution

  • The issue was not caused by a problem with the mapping, but due to a bug in the (quite old) version of EclipseLink the project was using, whereby a newly created object would not cascade changes made to an already existing object.

    https://bugs.eclipse.org/bugs/show_bug.cgi?id=340802

    Just a good reminder that whenever possible you should try to update the version of the dependencies in your project...