I am using Eclipselink and want to write changlog (an entity) for all insert/change/delete business enitities. I found the hooks postCalculateUnitOfWorkChangeSet or preCommitTransaction of SessionEventAdapter where I can build and insert changelog basing on the changeset. How can I get the changeset for insert/update/delete object in the two methods? How can I insert new changelog entities with the same unit of work because I need they happen in the same transaction.
What I tried is as below:
@Override
public void postCalculateUnitOfWorkChangeSet(SessionEvent event) {
Object source = event.getSource();
if (source instanceof UnitOfWork) {
UnitOfWork unitOfWork = (UnitOfWork) source;
UnitOfWorkChangeSet unitOfWorkChangeSet = unitOfWork.getUnitOfWorkChangeSet();
if (unitOfWorkChangeSet != null && unitOfWorkChangeSet.hasChanges()) {
Map allChangeSets = unitOfWorkChangeSet.getAllChangeSets();
allChangeSets.forEach((k, v) -> {
if (v instanceof ObjectChangeSet) {
ObjectChangeSet ocs = (ObjectChangeSet) v;
if (ocs.isNew()) {
Object unitOfWorkClone = ocs.getUnitOfWorkClone();
if (unitOfWorkClone instanceof Traceable) {
ChangeLog changeLog = getChangeLog(Operation.INSERT, (Traceable) unitOfWorkClone);
unitOfWork.registerNewObject(changeLog);
unitOfWork.getParent().insertObject(changeLog);
}
} else {
List<ChangeRecord> changes = ((ObjectChangeSet) v).getChanges();
changes.forEach(c -> {
Object unitOfWorkClone = ((ObjectChangeSet) (c.getOwner())).getUnitOfWorkClone();
if (unitOfWorkClone instanceof Traceable) {
ChangeLog changeLog = getChangeLog(Operation.UPDATE, (Traceable) unitOfWorkClone);
unitOfWork.getParent().insertObject(changeLog);
}
});
}
}
}
);
}
}
}
This can work but : 1. I am not sure if it is right way to get the changeset because I did not find document on this. 2. It cannot batch write. When I batch insert business objects, the business obejcts can be written in batch. But the changelogs are inserted one by one.
I also tried history policy. but it cannot work well with batch write. eclipselink batch write is disabled when use history policy or DescriptorEventAdapter
Thanks a lot for your any comments.