I'm trying to implement a validity-check on all new objects before the unit of work commits (we have "transient" objects with negative ids that should never be put into a relationship with persistent objects but sometime they are).
I thought I would be easy: register for the preCommitUnitOfWork event and examine every object in unitOfWork.getNewObjectsCloneToOriginal().
This approach turned out to have a problem: I only find the new objects that have been explicitely registered and not those that are persisted by reachability.
I looked some more and found the postCalculateUnitOfWorkChangeSet event, which seems to be a good place to do what I want. Only it doesn't. I still don't find all new objects.
I do find them after the commit (postCommitUnitOfWork event) but this is too late, since the database already contains junk.
Of course we can put a constraint in the database and we probaby will. But it still bugs me. What is the true eclipselink way to do this?
The non-registered new objects are contained in unregisteredNewObjects (uow.getUnregisteredNewObjects()), you may be able to find them there. You can also trigger them to be discovered if you call uow.assignSequenceNumbers().
You want to try using the preInsert DescriptorEvent instead.