如何配置EclipseLink HistoryPolicy以跟踪GlassFish 4中JPA实体的元素集合属性的更改?

时间:2013-12-10 01:41:54

标签: java jpa glassfish eclipselink

我在Person实体上成功配置了EclipseLink HistoryPolicy,并验证了在GlassFish 4上运行的EclipseLink跟踪了对PERSON_VERSION表中PERSON表的更改。然后,我使用Map属性扩展了我的Person实体,以保存该人的电话号码,如下面所示:

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

然后我尝试扩展我的DescriptorCustomizer类来跟踪对PERSON_TELEPHONENUMBER表的更改,如下面所示:

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);
    }
}

不幸的是,GlassFish拒绝部署我的应用程序。如果我从上面的PersonCustomizer类中注释掉mapping.setHistoryPolicy(policy2)行,则应用程序将进行部署,但不会在PERSON_VERSION_TELEPHONENUMBER表中跟踪对PERSON_TELEPHONENUMBER表的更改。

以下是GlassFish在应用程序部署期间报告的错误消息示例:

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)

...

谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

使用ManyToManyMapping可以实现这一点:

orm.xml中

<entity class="xxx.Client">
    <customizer class="xxx.ClientCustomizer"/>
...
            <many-to-many name="segments">
                <cascade>
                    <cascade-all/>
                </cascade>
                <join-table name="CLIENT_SEGMENTS">
                    <join-column name="CLIENTID" referenced-column-name="ID"/>
                    <inverse-join-column name="SEGMENTID" referenced-column-name="ID"  />
                </join-table>
            </many-to-many>

  ...
  </entity>

public class Client
    implements Serializable
{
...
       protected List<Segment> segments;
}
public class Segment
    implements Serializable
{

    @NotNull
    protected int id;
    protected String name;
    protected String description;
    protected String rule;
    ...
}

public class ClientCustomizer implements DescriptorCustomizer {

    @Override
    public void customize(ClassDescriptor descriptor) throws Exception {
        ManyToManyMapping mapping = (ManyToManyMapping) descriptor.getMappingForAttributeName("segments");
        HistoryPolicy policy = new HistoryPolicy();
        policy.addStartFieldName("ROW_START");
        policy.addEndFieldName("ROW_END");

        policy.addHistoryTableName("CLIENT_SEGMENTS", "CLIENT_SEGMENTS_HIST");
        mapping.setHistoryPolicy(policy);
    }

}

另见http://www.eclipse.org/forums/index.php/t/457024/