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ū)不一致的問題。
可在應(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)聽類。該注解可以用于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);
}
}
}
分析
同樣的從該類的注釋也可以了解到該類的作用:這是一個(gè)JPA Entity Listener,用于捕獲監(jiān)聽信息,當(dāng)Entity發(fā)生持久化和更新操作時(shí)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 深入詳解SpringBoot中時(shí)區(qū)問題解決與配置方案
- SpringBoot時(shí)區(qū)問題解決以及徹底解決時(shí)差問題
- springboot項(xiàng)目如何設(shè)置時(shí)區(qū)
- 如何解決springboot數(shù)據(jù)庫(kù)查詢時(shí)出現(xiàn)的時(shí)區(qū)差異問題
- SpringBoot如何根據(jù)用戶系統(tǒng)時(shí)區(qū)動(dòng)態(tài)展示時(shí)間
- springboot如何統(tǒng)一設(shè)置時(shí)區(qū)
- SpringBoot中?Jackson?日期的時(shí)區(qū)和日期格式問題解決
- 關(guān)于SpringBoot mysql數(shù)據(jù)庫(kù)時(shí)區(qū)問題
- SpringBoot時(shí)區(qū)設(shè)置的幾個(gè)方面小結(jié)
相關(guān)文章
關(guān)于Jackson的JSON工具類封裝 JsonUtils用法
這篇文章主要介紹了關(guān)于Jackson的JSON工具類封裝 JsonUtils用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-09-09
Java使用Lambda表達(dá)式實(shí)現(xiàn)排序功能的實(shí)現(xiàn)代碼
在 Java 中使用 Lambda 表達(dá)式進(jìn)行排序是一種簡(jiǎn)潔且功能強(qiáng)大的方式,特別是在對(duì)集合中的對(duì)象進(jìn)行自定義排序時(shí),Lambda 表達(dá)式可以與 Collections.sort() 或 List.sort() 方法結(jié)合使用,本文給大家介紹了Java使用Lambda表達(dá)式實(shí)現(xiàn)排序功能的方法,需要的朋友可以參考下2025-07-07
Java實(shí)現(xiàn)將html字符串插入到PPT幻燈片
Java后端代碼操作PPT幻燈片時(shí),可直接在幻燈片中繪制形狀,并在形狀中添加文本字符串內(nèi)容。本篇文章主要介紹通過java實(shí)現(xiàn)將html字符串添加到PPT幻燈片的的方法,可添加文字、圖片、視頻、音頻等。以下是具體方法和步驟。2021-11-11
java底層AQS實(shí)現(xiàn)類ReentrantLock鎖的構(gòu)成及源碼解析
本章我們就要來(lái)學(xué)習(xí)一下第一個(gè)?AQS?的實(shí)現(xiàn)類:ReentrantLock,看看其底層是如何組合?AQS?,實(shí)現(xiàn)了自己的那些功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03

