How do you configure an EclipseLink HistoryPolicy to track changes to an Element Collection Property of a JPA Entity in GlassFish 4?

Viewed 1392

I successfully configured an EclipseLink HistoryPolicy on a Person entity and verified that EclipseLink running on GlassFish 4 tracked changes to the PERSON table in the PERSON_VERSION table. I then extended my Person entity with a Map property to hold the person's telephone numbers as shown in the following class:

@Customizer(PersonCustomizer.class)
public class Person {
    @ID
    private Long id;
    @Version
    private Integer version;
    ....
    @ElementCollection
    @MapKeyEnumerated(EnumType.STRING)
    private Map<TelephoneNumberType, String> telephoneNumber;
    ...
}

I then tried to extend my DescriptorCustomizer class to track the changes to the PERSON_TELEPHONENUMBER table as show in the following class:

public class PersonCustomizer implements DescriptorCustomizer {
    public void customize(ClassDescriptor descriptor) throws Exception {
        HistoryPolicy policy = new HistoryPolicy();
        policy.addStartFieldName("VALIDFROM");
        policy.addEndFieldName("VALIDTO");
        policy.setShouldHandleWrites(true);
        policy.addHistoryTableName("PERSON", "PERSON_VERSION");
        descriptor.setHistoryPolicy(policy);

        DirectMapMapping mapping = (DirectMapMapping) descriptor.getMappingForAttributeName("telephoneNumber");
        HistoryPolicy policy2 = new HistoryPolicy();
        policy2.setShouldHandleWrites(true);
        policy2.addHistoryTableName("PERSON_TELEPHONENUMBER", "PERSON_VERSION_TELEPHONENUMBER");
        mapping.setHistoryPolicy(policy2);
    }
}

Unfortunately, GlassFish refuses to deploy my application. If I comment out the mapping.setHistoryPolicy(policy2) line from the above PersonCustomizer class, the application deploys but changes to the PERSON_TELEPHONENUMBER table are not tracked in the PERSON_VERSION_TELEPHONENUMBER table.

The following is a sample of the error message that GlassFish reports during application deployment:

Local Exception Stack: 
Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-41] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A non-read-only mapping must be defined for the sequence number field.
Descriptor: RelationalDescriptor(Person --> [DatabaseTable(PERSON)])

Runtime Exceptions: 
---------------------------------------------------------

java.lang.NullPointerException

    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:689)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:625)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:565)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:792)

...

Descriptor Exceptions: 
---------------------------------------------------------

Local Exception Stack: 
Exception [EclipseLink-41] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A non-read-only mapping must be defined for the sequence number field.
Descriptor: RelationalDescriptor(Person --> [DatabaseTable(PERSON)])
    at org.eclipse.persistence.exceptions.DescriptorException.mappingForSequenceNumberField(DescriptorException.java:929)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.validate(ObjectBuilder.java:4090)
    at org.eclipse.persistence.descriptors.ClassDescriptor.selfValidationAfterInitialization(ClassDescriptor.java:4061)
    at org.eclipse.persistence.descriptors.ClassDescriptor.validateAfterInitialization(ClassDescriptor.java:5910)

...

Runtime Exceptions: 
---------------------------------------------------------


java.lang.NullPointerException
    at org.eclipse.persistence.history.HistoryPolicy.initialize(HistoryPolicy.java:320)
    at org.eclipse.persistence.mappings.DirectCollectionMapping.initialize(DirectCollectionMapping.java:1457)
    at org.eclipse.persistence.mappings.DirectMapMapping.initialize(DirectMapMapping.java:367)
    at org.eclipse.persistence.descriptors.ClassDescriptor.initialize(ClassDescriptor.java:2980)

...

Thank you for you help.

1 Answers
Related