我在 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)
...
谢谢你的帮助。
最佳答案
这可以通过 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/
关于java - 如何配置 EclipseLink HistoryPolicy 以跟踪 GlassFish 4 中 JPA 实体的元素集合属性的更改?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20484458/