10

I'm trying to include a changes tracker to my JPA Entitys (to a log file, not a database) however the changeSet returned by my DescriptorEventAdapter is always null. I'm using EclipseLink 2.5.2, ojdbc6, spring-orm 4.1.1.

All events are called (including preUpdateWithChanges) and the changes are pushed to the database. I'm using entityManager.merge(entity) to update the entity.

HistoryEventListener.java

public class HistoryEventListener extends DescriptorEventAdapter {
    @Override
    public void preUpdate(DescriptorEvent event) {
        ObjectChangeSet changeSet = event.getChangeSet(); // Always null
    }

    @Override
    public void preUpdateWithChanges(DescriptorEvent event) {
        ObjectChangeSet changeSet = event.getChangeSet();
        ...
    };

    @Override
    public void postUpdate(DescriptorEvent event) {
        ...
    }

    @Override
    public void postMerge(DescriptorEvent event) {
        ...
    }
}

Some entity

@Entity
@Table(name = "XXX", schema = "XXX")
@EntityListeners(HistoryEventListener.class)
@Cache(databaseChangeNotificationType = DatabaseChangeNotificationType.NONE, isolation = CacheIsolationType.ISOLATED)
public class XXXX implements Serializable {
  // id + fields
}

persistence.xml

<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="XXXXXX"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>jdbc/XXXXX</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.weaving" value="static" />
            <property name="eclipselink.target-database" value="Oracle11" />
        </properties>
    </persistence-unit>
</persistence>

Using UnitOfWork from eclipse wiki also returns a Null ObjectChangeSet.

martinyyyy
  • 1,551
  • 3
  • 18
  • 42

2 Answers2

4

Reading the event.getQuery() works. No idea why the event.getChangeSet() is emtpy however this is how I solved it.

public void preUpdateWithChanges(DescriptorEvent event) {
    if (event.getQuery() instanceof UpdateObjectQuery) {
        UpdateObjectQuery query = (UpdateObjectQuery) event.getQuery();
        for (ChangeRecord cr : query.getObjectChangeSet().getChanges()) {
            String clazz = query.getObject().getClass().getSimpleName();
            Object id = query.getObjectChangeSet().getId();
            Object newValue = PropertyUtils.getProperty(query.getObject(), cr.getAttribute());
            Object oldVal = cr.getOldValue();
    }
}
martinyyyy
  • 1,551
  • 3
  • 18
  • 42
0

From the API documentation for DescriptorEventAdapter.preUpdate:

This event is raised before the object's changes are computed....

(The bolding is my addition.)

From the documentation for DescriptorEvent.changeSet:

For the post merge event it is possible that there has been a change set generated.

What happens in your postMerge() override?

Ken Clubok
  • 1,198
  • 5
  • 11