SpringBoot:JPA + AuditingEntityListener時(shí)區(qū)設(shè)置方式
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>
* <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);
}
}
}
分析
同樣的從該類(lèi)的注釋也可以了解到該類(lèi)的作用:這是一個(gè)JPA Entity Listener,用于捕獲監(jiān)聽(tīng)信息,當(dāng)Entity發(fā)生持久化和更新操作時(shí)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 如何解決springboot數(shù)據(jù)庫(kù)查詢時(shí)出現(xiàn)的時(shí)區(qū)差異問(wèn)題
- SpringBoot如何根據(jù)用戶系統(tǒng)時(shí)區(qū)動(dòng)態(tài)展示時(shí)間
- springboot如何統(tǒng)一設(shè)置時(shí)區(qū)
- SpringBoot中?Jackson?日期的時(shí)區(qū)和日期格式問(wèn)題解決
- 關(guān)于SpringBoot mysql數(shù)據(jù)庫(kù)時(shí)區(qū)問(wèn)題
- springboot項(xiàng)目如何設(shè)置時(shí)區(qū)
相關(guān)文章
java中CompletableFuture異步執(zhí)行方法
本文主要介紹了java中CompletableFuture異步執(zhí)行方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
引入mybatis-plus報(bào) Invalid bound statement錯(cuò)誤問(wèn)題的解決方法
這篇文章主要介紹了引入mybatis-plus報(bào) Invalid bound statement錯(cuò)誤問(wèn)題的解決方法,需要的朋友可以參考下2020-05-05
Spring?Bean注冊(cè)與注入實(shí)現(xiàn)方法詳解
首先,要學(xué)習(xí)Spring中的Bean的注入方式,就要先了解什么是依賴注入。依賴注入是指:讓調(diào)用類(lèi)對(duì)某一接口的實(shí)現(xiàn)類(lèi)的實(shí)現(xiàn)類(lèi)的依賴關(guān)系由第三方注入,以此來(lái)消除調(diào)用類(lèi)對(duì)某一接口實(shí)現(xiàn)類(lèi)的依賴。Spring容器中支持的依賴注入方式主要有屬性注入、構(gòu)造函數(shù)注入、工廠方法注入2022-10-10
java poi設(shè)置生成的word的圖片為上下型環(huán)繞以及其位置的實(shí)現(xiàn)
這篇文章主要介紹了java poi設(shè)置生成的word的圖片為上下型環(huán)繞以及其位置的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
SpringCloud+SpringBoot項(xiàng)目搭建結(jié)構(gòu)層次的實(shí)例
這篇文章詳細(xì)介紹了SpringCloud項(xiàng)目的架構(gòu)層次及其搭建經(jīng)驗(yàn),包括Controller層、Service層、Repository層、Entity層、DTO層、Exception層等,通過(guò)文字和圖片的形式,幫助讀者理解如何組織和實(shí)現(xiàn)一個(gè)SpringBoot項(xiàng)目的不同層次2025-01-01
java?-jar啟動(dòng)參數(shù)設(shè)置file.encoding編碼,解決中文亂碼的問(wèn)題
這篇文章主要介紹了java?-jar啟動(dòng)參數(shù)設(shè)置file.encoding編碼,解決中文亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
springboot集成@DS注解實(shí)現(xiàn)數(shù)據(jù)源切換的方法示例
本文主要介紹了springboot集成@DS注解實(shí)現(xiàn)數(shù)據(jù)源切換的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

