SpringBoot:JPA + AuditingEntityListener時區(qū)設置方式
JPA + AuditingEntityListener時區(qū)設置
在SpringBoot項目中,如果應用啟用了EnableJpaAuditing并且使用AuditingEntityListener對實體的創(chuàng)建時間、更新時間進行自動審計,可能存在生成時間的時區(qū)和系統(tǒng)時區(qū)不一致的問題。
可在應用配置中添加如下配置
將時區(qū)設定為指定時區(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(); }
分析
從源碼的注釋中可以很清楚的了解到該注解的作用,簡單翻譯如下:該注解用于指定Entity或者superclass上的回調(diào)監(jiān)聽類。該注解可以用于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,用于捕獲監(jiān)聽信息,當Entity發(fā)生持久化和更新操作時。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
idea熱部署插件jrebel正式版及破解版安裝詳細圖文教程
這篇文章主要介紹了idea熱部署插件jrebel正式版及破解版安裝詳細教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12使用springboot開發(fā)的第一個web入門程序的實現(xiàn)
這篇文章主要介紹了使用springboot開發(fā)的第一個web入門程序的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04