Wildfly 9.0.2.Final上的Deltaspike Ver 1.7.2
我在实体bean上使用JPA @EntityListeners(AuditEntityListener.class)
和Deltaspike Data(@CreatedOn
,@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
}
@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是否正确 - &gt; UTCDateTimeAuditListener
并使用@EntityListeners(UTCDateTimeAuditEntityListener.class)
,其中UTCDateTimeAuditListener
遵循org.apache.deltaspike.data.impl.audit.AuditEntityListener
方法?
其次,我是否需要在某处使用CDI限定符,以确保我的UTCDateTimeAuditEntityListener
获得对知道如何构造{{PrePersistAuditListener
和PreUpdateAuditListener
实现的正确引用的引用1}}实例?
最后,我不知道它是否相关,但UTCDateTime
适合这种情况?
我在回答我的问题方面取得了一些进展。到目前为止我做了什么。
没有必要使用DeltaSpike提供的自定义org.apache.deltaspike.data.impl.audit.TimestampsProvider
就足够了。
我创建了一个AuditEntityListener
的新实现,它能够处理我的TimestampsProvider
个对象。
UTCDateTime
beans.xml还将此列为CDI @Alternative
@Alternative
@Dependent
public class UTCDateTimeAuditProvider implements PrePersistAuditListener, PreUpdateAuditListener {
// implementation details not important
}
<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
我尝试过各种组合,只用注释来指定我的@Alternative实现,或者仅在beans.xml中指定我的@Alternative实现,并且在类和&amp;在beans.xml。
有人知道为什么在@Alternative实现后执行默认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
?
Deltaspike Ver 1.7.2 on Wildfly 9.0.2.Final with Weld 2.2.16(SP1)
答案 0 :(得分:0)
Deltaspike数据审核不支持Java 8日期和时间API。 您可以将@ PrePersist,@ PreUpdate方法与Deltaspike @ModifiedBy字段组合为所有可审核实体的一个共同祖先(好处:不需要获取主体名称的自定义方式)。