Skip to main content

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

I am replying to myself here because I figured it out.

Here is the solution:

            InsertObjectQuery query = (InsertObjectQuery) ev.getQuery();
            for (int i = 0; i < query.getModifyRow().getFields().size(); i++)
            {
                DatabaseField dbField =
(DatabaseField)query.getModifyRow().getFields().elementAt(i);
                if (dbField == null)
                    continue;
                String fieldName = dbField.getName();
                if (query.getModifyRow().getValues(fieldName) instanceof String)
                {
                    String valueName = (String)
query.getModifyRow().getValues(fieldName);
                    if (valueName == null)
                        continue;
                }
             }

Note that in this example, I only want to consider the fields that are
of type "String".  Now the fieldName (column name in the SQL table)
and its value are available to me.

Sincerely,
Torbjorn


On Tue, Aug 17, 2010 at 4:14 PM, Torbjorn Kristoffersen
<torbjoern@xxxxxxxxx> wrote:
> 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