温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot中JPA + AuditingEntityListener时区设置方式是什么

发布时间:2021-12-21 13:01:43 来源:亿速云 阅读:249 作者:柒染 栏目:开发技术

这篇文章将为大家详细讲解有关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>
 * &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发生持久化和更新操作时。

关于SpringBoot中JPA + AuditingEntityListener时区设置方式是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI