we have two entities like Resource and NonFTECost. And we have relationship between these is OneToMany bi-directional. below is the entities
@Getter
@Setter
@NoArgsConstructor
@Entity
@Table
public class Resource {
@NotNull
@Column(name = "NAME", nullable = false, length = 255)
@Audited(withModifiedFlag = true)
private String name;
@OneToMany(mappedBy = "resource", cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
@Audited
private List<NonFTECost> costings = new ArrayList<>();
//other fields
}
@Getter
@Setter
@Table
@Entity
public class NonFTECost {
@NotNull
@Audited(withModifiedFlag = true, modifiedColumnName = "PAYMENT_MOD")
private Payment payment;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "RESOURCE_CODE", nullable = false, foreignKey = @ForeignKey(name = FK_COST_DETAILS_RESOURCE_CODE))
@Audited(withModifiedFlag = true, modifiedColumnName = "RESOURCE_CODE_MOD", targetAuditMode = RelationTargetAuditMode.AUDITED)
private Resource resource;
//other fields
}
now i create the one resource wiht costings, then it will create new revision for each audit table. And then i changed only payment field of NonFTECost entity, it will create new revision in NonFTECost_Aud table(it is also the part of resource update).
Question:- While getting the revision of resource, i want to get revisions of NonFTECost for that perticular resource entity. because of i want to show to user like fieldName oldvalue newvalue
Please help me to sort out the issue.
You should be able to fetch the associated
NonFTECost
entities for a given revision ofResource
by iterating that collection on the specific revision instance you queried.For example, lets say I was interested in revision 5 of
Resource
Now what you need is how to take that
Cost
instance and find out what changed. Since you use the featurewithModifiedFlags=true
, this allows us to make special use offorRevisionsOfEntityWithChanges
.The one thing I want to point out is that it is possible in your mapping scenario that a
NonFTECost
entity could have a higher revision than that of yourResource
if you happen to modify the cost entity in a transaction where no modification happens specifically toResource
.With that in mind, you'll need to account for that in the for-loop logic. So inside that loop, we'll need to execute a query based on the
Cost
instance and fetch its revision history separately.If the results list only contains 1 row, then you know a couple of things (assuming no data pruning)
Set<String>
of fields set during the original persist.If the results list contains 2 rows, then this is where you'll need to handle old and new value logic. You should be able to do something like this:
This isn't the most elegant of solutions, but it works.