SpringBoot:JPA + AuditingEntityListener时区设置方式
JPA + AuditingEntityListener时区设置
在SpringBoot项目中,如果应用启用了EnableJpaAuditing并且使用AuditingEntityListener对实体的创建时间、更新时间进行自动审计,可能存在生成时间的时区和系统时区不一致的问题。
可在应用配置中添加如下配置
将时区设定为指定时区:
spring.jpa.properties.hibernate.jdbc.time_zone = GMT+8
@EntityListeners(AuditingEntityListener.class)介绍
@EntityListeners
源码
/**
* Specifies the callback listener classes to be used for an
* entity or mapped superclass. This annotation may be applied
* to an entity class or mapped superclass.
*
* @since Java Persistence 1.0
*/
@Target({TYPE})
@Retention(RUNTIME)
public @interface EntityListeners {
/** The callback listener classes */
Class[] value();
}
分析
从源码的注释中可以很清楚的了解到该注解的作用,简单翻译如下:该注解用于指定Entity或者superclass上的回调监听类。该注解可以用于Entity或者superclass上。
AuditingEntityListener.class
源码
/**
* JPA entity listener to capture auditing information on persiting and updating entities. To get this one flying be
* sure you configure it as entity listener in your {@code orm.xml} as follows:
*
* <pre>
* <persistence-unit-metadata>
* <persistence-unit-defaults>
* <entity-listeners>
* <entity-listener class="org.springframework.data.jpa.domain.support.AuditingEntityListener" />
* </entity-listeners>
* </persistence-unit-defaults>
* </persistence-unit-metadata>
* </pre>
*
* After that it's just a matter of activating auditing in your Spring config:
*
* <pre>
* @Configuration
* @EnableJpaAuditing
* class ApplicationConfig {
*
* }
* </pre>
*
* <pre>
* <jpa:auditing auditor-aware-ref="yourAuditorAwarebean" />
* </pre>
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
@Configurable
public class AuditingEntityListener {
private ObjectFactory<AuditingHandler> handler;
/**
* Configures the {@link AuditingHandler} to be used to set the current auditor on the domain types touched.
*
* @param auditingHandler must not be {@literal null}.
*/
public void setAuditingHandler(ObjectFactory<AuditingHandler> auditingHandler) {
Assert.notNull(auditingHandler, "AuditingHandler must not be null!");
this.handler = auditingHandler;
}
/**
* Sets modification and creation date and auditor on the target object in case it implements {@link Auditable} on
* persist events.
*
* @param target
*/
@PrePersist
public void touchForCreate(Object target) {
if (handler != null) {
handler.getObject().markCreated(target);
}
}
/**
* Sets modification and creation date and auditor on the target object in case it implements {@link Auditable} on
* update events.
*
* @param target
*/
@PreUpdate
public void touchForUpdate(Object target) {
if (handler != null) {
handler.getObject().markModified(target);
}
}
}
分析
同样的从该类的注释也可以了解到该类的作用:这是一个JPA Entity Listener,用于捕获监听信息,当Entity发生持久化和更新操作时。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
引入mybatis-plus报 Invalid bound statement错误问题的解决方法
这篇文章主要介绍了引入mybatis-plus报 Invalid bound statement错误问题的解决方法,需要的朋友可以参考下2020-05-05
java poi设置生成的word的图片为上下型环绕以及其位置的实现
这篇文章主要介绍了java poi设置生成的word的图片为上下型环绕以及其位置的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-09-09
SpringCloud+SpringBoot项目搭建结构层次的实例
这篇文章详细介绍了SpringCloud项目的架构层次及其搭建经验,包括Controller层、Service层、Repository层、Entity层、DTO层、Exception层等,通过文字和图片的形式,帮助读者理解如何组织和实现一个SpringBoot项目的不同层次2025-01-01
java -jar启动参数设置file.encoding编码,解决中文乱码的问题
这篇文章主要介绍了java -jar启动参数设置file.encoding编码,解决中文乱码的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-07-07


最新评论