Wildfly 9.0.2.Final 上的 Deltaspike Ver 1.7.2
我在实体 bean 上使用 JPA 和 Deltaspike 数据(和注释)@EntityListeners(AuditEntityListener.class)
的审计功能,@CreatedOn
不同之处在于我有一个自定义实现,它将任何+ OR a转换为 UTC 日期和时间。@ModifiedOn
@ModifiedBy
java.time.ChronoLocalDateTime
LocalDateTime
ZoneOffset
ZonedDateTime
UTC日期时间
public class UTCDateTime implements ChronoLocalDateTime<LocalDate>, Serializable {
private static final long serialVersionUID = 6492792765662073566L;
private static final ZoneOffset UTC = ZoneOffset.UTC;
private final LocalDateTime datetime;
// a lot of other details left out
}
实体 bean 部分
@MappedSuperclass
public class InsertableAuditableBean extends BaseBean implements InsertableAuditable {
@NotNull
@Size(min = 1, max = 50)
@Column(name = "zz_inserted_by", length = 50, nullable = false)
private String insertedBy;
@CreatedOn
@NotNull
@Temporal(value = TemporalType.TIMESTAMP)
@Column(name = "zz_inserted_on", nullable = false)
private UTCDateTime insertedOn;
// getters & setters
}
@MappedSuperclass
public class UpdateableAuditableBean extends InsertableAuditableBean implements UpdateableAuditable {
@ModifiedBy
@Size(min = 1, max = 50)
@Column(name = "zz_updated_by", length = 50, nullable = true)
private String updatedBy;
@ModifiedOn
@Temporal(value = TemporalType.TIMESTAMP)
@Column(name = "zz_updated_on", nullable = true)
private UTCDateTime updatedOn;
// getters & setters
}
@Entity
@EntityListeners(AuditEntityListener.class)
@Table(schema = "data", name = "manufacturer", uniqueConstraints = {
@UniqueConstraint(columnNames = { "man_name", "man_country" })
})
@AttributeOverrides({
@AttributeOverride(name = "primaryKey", column = @Column(name = "man_serial")),
@AttributeOverride(name = "insertedBy", column = @Column(name = "man_inserted_by")),
@AttributeOverride(name = "insertedOn", column = @Column(name = "man_inserted_on")),
@AttributeOverride(name = "updatedBy", column = @Column(name = "man_updated_by")),
@AttributeOverride(name = "updatedOn", column = @Column(name = "man_updated_on"))
})
@SequenceGenerator(name = "default_seq", schema = "data", sequenceName = "manufacturer_man_serial_seq",
allocationSize = 1)
public class Manufacturer extends MirroredUpdateableAuditableBean implements IManufacturer {
// nothing special here
}
UTCDateTime 类还有一个自定义的 AttributeConverter,因为 epoch 值保存在数据库中。
@Converter(autoApply = true)
public class UTCDateTimePersistenceConverter implements AttributeConverter<UTCDateTime, Long> {
@Override
public Long convertToDatabaseColumn(final UTCDateTime entityValue) {
Long res = null;
if (entityValue != null) {
res = entityValue.toMillis();
}
return res;
}
@Override
public UTCDateTime convertToEntityAttribute(final Long databaseValue) {
UTCDateTime res = null;
if (databaseValue != null) {
res = new UTCDateTime(Instant.ofEpochMilli(databaseValue));
}
return res;
}
}
现在,当我坚持实体时,我得到以下异常(真正原因的最后一点):
Caused by: org.apache.deltaspike.data.api.QueryInvocationException: Failed calling Repository: [Repository=systems.apace.data.manufacturer.model.dao.ManufacturerDAO,entity=systems.apace.data.manufacturer.model.Manufacturer,method=persist,exception=class java.lang.reflect.InvocationTargetException,message=null
at systems.apace.data.manufacturer.services.ManufacturerServiceBeanIntegrationTest.testInsertBean(ManufacturerServiceBeanIntegrationTest.java:55)
Caused by: java.lang.reflect.InvocationTargetException
at systems.apace.data.manufacturer.services.ManufacturerServiceBeanIntegrationTest.testInsertBean(ManufacturerServiceBeanIntegrationTest.java:55)
Caused by: org.apache.deltaspike.data.impl.audit.AuditPropertyException: Failed to set property Manufacturer.insertedOn, is this a temporal type?
at systems.apace.data.manufacturer.services.ManufacturerServiceBeanIntegrationTest.testInsertBean(ManufacturerServiceBeanIntegrationTest.java:55)
Caused by: java.lang.IllegalArgumentException: Annotated field is not a date class: class za.co.t9.common.utils.time.UTCDateTime
at systems.apace.data.manufacturer.services.ManufacturerServiceBeanIntegrationTest.testInsertBean(ManufacturerServiceBeanIntegrationTest.java:55)
有没有办法实现我自己的org.apache.deltaspike.data.impl.audit.PrePersistAuditListener
并org.apache.deltaspike.data.impl.audit.PreUpdateAuditListener
使用它们来创建实例UTCDateTime
?
编写我自己的 EntityListener -->UTCDateTimeAuditListener
并在遵循方法的@EntityListeners(UTCDateTimeAuditEntityListener.class)
地方使用它是否正确?UTCDateTimeAuditListener
org.apache.deltaspike.data.impl.audit.AuditEntityListener
其次,我是否需要在某处使用 CDI 限定符以确保我UTCDateTimeAuditEntityListener
获得对知道如何构造实例的正确PrePersistAuditListener
和实现的引用?PreUpdateAuditListener
UTCDateTime
最后,我不知道它是否相关,但在哪里org.apache.deltaspike.data.impl.audit.TimestampsProvider
适合这种情况?
更新
我在回答我的问题方面取得了一些进展。到目前为止我所做的。不需要定制AuditEntityListener
,DeltaSpike 提供的定制就足够了。
我创建了一个TimestampsProvider
能够处理我的UTCDateTime
对象的新实现。
@Alternative
@Dependent
public class UTCDateTimeAuditProvider implements PrePersistAuditListener, PreUpdateAuditListener {
// implementation details not important
}
beans.xml 还将其列为 CDI @Alternative
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="annotated">
<alternatives>
<class>systems.apace.data.manufacturer.model.UTCDateTimeAuditProvider</class>
</alternatives>
</beans>
内容apache-deltaspike.properties
globalAlternatives.org.apache.deltaspike.jpa.spi.transaction.TransactionStrategy=org.apache.deltaspike.jpa.impl.transaction.ContainerManagedTransactionStrategy
现在的问题是两个AuditProvider
实例都被执行了,首先是我的实现(UTCDateTimeAuditProvider
)然后是默认的TimestampsProvider
2016-12-13 01:29:22,929 INFO Apace-BPS s.a.d.m.m.UTCDateTimeAuditProvider [ServerService Thread Pool -- 319] [Unknown Id] - prePersist: class systems.apace.data.manufacturer.model.UTCDateTimeAuditProvider
2016-12-13 01:29:22,931 INFO Apace-BPS s.a.d.m.m.UTCDateTimeAuditProvider [ServerService Thread Pool -- 319] [Unknown Id] - Updated property Manufacturer.insertedOn with 2016-12-12T23:29:22.930
2016-12-13 01:29:22,932 INFO Apace-BPS z.c.t.c.b.e.EntityManagerProducer [ServerService Thread Pool -- 319] [Unknown Id] - username of Principal: anonymous
2016-12-13 01:29:22,932 FINER [org.apache.deltaspike.data.impl.audit.AuditProvider] (ServerService Thread Pool -- 319) Updated Manufacturer.updatedBy with anonymous
2016-12-13 01:29:22,935 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 319) MSC000001: Failed to start service jboss.deployment.subunit."bpsserver.ear"."systems-apace-data-model-1.0-SNAPSHOT.jar".component.ManufacturerSi
ngleton.START: org.jboss.msc.service.StartException in service jboss.deployment.subunit."bpsserver.ear"."systems-apace-data-model-1.0-SNAPSHOT.jar".component.ManufacturerSingleton.START: java.lang.IllegalStateException: WFLYEE0042: Failed
to construct component instance
at org.jboss.as.ee.component.ComponentStartService$1.run(ComponentStartService.java:57)
...
Caused by: org.apache.deltaspike.data.impl.audit.AuditPropertyException: Failed to set property Manufacturer.insertedOn, is this a temporal type?
at org.apache.deltaspike.data.impl.audit.TimestampsProvider.setProperty(TimestampsProvider.java:86)
at org.apache.deltaspike.data.impl.audit.TimestampsProvider.updateTimestamps(TimestampsProvider.java:67)
at org.apache.deltaspike.data.impl.audit.TimestampsProvider.prePersist(TimestampsProvider.java:43)
at org.apache.deltaspike.data.impl.audit.AuditEntityListener.persist(AuditEntityListener.java:42)
...
Caused by: java.lang.IllegalArgumentException: Annotated field is not a date class: class za.co.t9.common.utils.time.UTCDateTime
at org.apache.deltaspike.data.impl.audit.TimestampsProvider.now(TimestampsProvider.java:115)
at org.apache.deltaspike.data.impl.audit.TimestampsProvider.setProperty(TimestampsProvider.java:79)
... 115 more
我尝试了各种组合,仅使用注释指定我的@Alternative 实现,或者仅在 beans.xml 中以及在类和 beans.xml 中。
有谁知道为什么TimestampsProvider
在我的@Alternative 实现之后执行默认值?
Wildfly 9.0.2.Final 和 Weld 2.2.16 (SP1) 上的 Deltaspike Ver 1.7.2