I have 2-level deep all-unidirectional object hierarchy with collections and auditing that I am trying to save using hibernate and envers. The code is failing to save it with a NPE in envers if I use saveOrUpdate, or a NonUniqueObjectException if I try to save using merge. This looks like an issue with save of multi-column join, with a parent of two primary columns inserting into a child of three primary columns, but I am not sure.
Here is my code:
First Level Class Borrower with two columns in primary key:
Code:
@Audited
@Entity
public class Borrower implements Serializable{
@Id
@Column(name = "BWR_ID", nullable = false)
private String borrowerId;
@Id
@Column(name = "LOAN_ID")
String loanId;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumns({
@JoinColumn(name = "LOAN_ID"),
@JoinColumn(name = "BWR_ID")})
@NotAudited
Set<BorrowerRating> borrowerRatings;
... getters, setters and other fields
}
Second Level class BorrowerRating, with an additional column as primary key
Code:
@Audited
@Entity
public class BorrowerRating implements Serializable{
@Id
@Column(name = "LOAN_ID")
private String loanId;
@Id
@Column(name = "BWR_ID", nullable = false)
private String borrowerId;
@Id
@Column(name = "RATING_EFECTIVE_DT")
private String ratingEffectiveDate;
... getters, setters and other fields
}
In my test, if I try to "update" graph using merge, I get
Quote:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.a.b.c.d.BorrowerRating#BorrowerRating [loanId=null, borrowerId=null, ratingEffectiveDate=null]]
Debugging shows it identified BorrowerRating as detached object and while creating clone, created an empty object (as evident above with all values null), even though my original object has all values populated. When it encountered the second object from set, it created another empty object - and obviously resulted in NonUniqueObjectException.
Instead, if I try to update it using saveOrUpdate, I get:
Quote:
java.lang.NullPointerException
at org.hibernate.envers.event.EnversPostUpdateEventListenerImpl.postUpdateDBState(EnversPostUpdateEventListenerImpl.java:86)
at org.hibernate.envers.event.EnversPostUpdateEventListenerImpl.onPostUpdate(EnversPostUpdateEventListenerImpl.java:53)
at org.hibernate.action.internal.EntityUpdateAction.postUpdate(EntityUpdateAction.java:255)
at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:212)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
Neither of these make any sense to me. Can someone help me understand what am I doing wrong here? TIA!