亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot:JPA + AuditingEntityListener時(shí)區(qū)設(shè)置方式

 更新時(shí)間:2021年12月21日 11:06:11   作者:晴空排云  
這篇文章主要介紹了SpringBoot:JPA + AuditingEntityListener時(shí)區(qū)設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

JPA + AuditingEntityListener時(shí)區(qū)設(shè)置

在SpringBoot項(xiàng)目中,如果應(yīng)用啟用了EnableJpaAuditing并且使用AuditingEntityListener對(duì)實(shí)體的創(chuàng)建時(shí)間、更新時(shí)間進(jìn)行自動(dòng)審計(jì),可能存在生成時(shí)間的時(shí)區(qū)和系統(tǒng)時(shí)區(qū)不一致的問(wèn)題。

可在應(yīng)用配置中添加如下配置

將時(shí)區(qū)設(shè)定為指定時(shí)區(qū):

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();
}

分析

從源碼的注釋中可以很清楚的了解到該注解的作用,簡(jiǎn)單翻譯如下:該注解用于指定Entity或者superclass上的回調(diào)監(jiān)聽(tīng)類(lèi)。該注解可以用于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);
  }
 }
}

分析

同樣的從該類(lèi)的注釋也可以了解到該類(lèi)的作用:這是一個(gè)JPA Entity Listener,用于捕獲監(jiān)聽(tīng)信息,當(dāng)Entity發(fā)生持久化和更新操作時(shí)。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論