Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Intercepting entities using postInsert(), but I need to get all fields and values

I'm using the "eclipselink.descriptor.customizer.MyEntity" trick to be
able to run code after an entity has been created, deleted, updated,
and so on.  That allows me to intercept right after an entity object
has been updated/deleted/inserted.

I created a class called MyHandler which extends
DescriptorEventAdapter and implements DescriptorCustomizer.  In this
class I implement the methods called "postInsert" and "postUpdate".
Both of them give me a DescriptorEvent object which I can use to find
out more about the persisted action.

For the "updated entities" (i.e. inside postUpdate), I can use the
following code to get the fields that were changed:

	WriteObjectQuery query = (WriteObjectQuery) ev.getQuery();

	List<ChangeRecord> changes = query.getObjectChangeSet().getChanges();

	for (ChangeRecord crec : changes)
	{
                if (crec instanceof DirectToFieldChangeRecord)
                {
                    DirectToFieldChangeRecord fieldChange =
(DirectToFieldChangeRecord) crec;

                    System.out.println("The field " +
fieldChange.getAttribute() +
                        " changed to: " + fieldChange.getNewValue().toString());
                }
            }

However, that only works for UPDATE's, not for INSERTs.  If I do the
same code for INSERTs (using InsertObjectQuery), I get zero
ChangeRecord objects.

That is sort of to be expected since technically they aren't
"changed", but rather "created for the first time".

How do I get all the fields/values for an INSERT'd (i.e. newly
persisted) entity via the DescriptorEvent object?

Sincerely,
Torbjorn


Back to the top