欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

SpringBoot:JPA + AuditingEntityListener时区设置方式

时间:2022-03-13 12:55:35|栏目:JAVA代码|点击:

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>
 * &lt;persistence-unit-metadata&gt;
 *     &lt;persistence-unit-defaults&gt;
 *         &lt;entity-listeners&gt;
 *             &lt;entity-listener class="org.springframework.data.jpa.domain.support.AuditingEntityListener" /&gt;
 *         &lt;/entity-listeners&gt;
 *     &lt;/persistence-unit-defaults&gt;
 * &lt;/persistence-unit-metadata&gt;
 * </pre>
 * 
 * After that it's just a matter of activating auditing in your Spring config:
 * 
 * <pre>
 * &#064;Configuration
 * &#064;EnableJpaAuditing
 * class ApplicationConfig {
 * 
 * }
 * </pre>
 * 
 * <pre>
 * &lt;jpa:auditing auditor-aware-ref="yourAuditorAwarebean" /&gt;
 * </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发生持久化和更新操作时。

上一篇:Java动态代理分析及理解

栏    目:JAVA代码

下一篇:从搭建Struts2 开发环境说起

本文标题:SpringBoot:JPA + AuditingEntityListener时区设置方式

本文地址:http://www.codeinn.net/misctech/196055.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有