@CreatedDateによる監査
エンティティの作成と変更を自動的に追跡します
「@CreatedDateによる監査」はCoddyKit上の無料Spring Boot 4 Microservices & REST APIsレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSpring Boot 4 Microservices & REST APIs学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
What is JPA Auditing
Auditing automatically tracks who created or modified an entity and when, without manual code in every save.
Spring Data JPA fills audit fields on insert and update for you.
Enabling Auditing
Add @EnableJpaAuditing to a configuration class to activate the feature.
@Configuration
@EnableJpaAuditing
public class JpaConfig {
}Registering the Listener
Annotate audited entities with @EntityListeners(AuditingEntityListener.class) so the callbacks fire.
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Product {
@Id @GeneratedValue
private Long id;
}@CreatedDate
@CreatedDate is set once when the entity is first persisted.
@CreatedDate
private Instant createdAt;@LastModifiedDate
@LastModifiedDate updates on every save, including updates.
@LastModifiedDate
private Instant updatedAt;A Complete Audited Entity
Putting the timestamp fields together.
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Product {
@Id @GeneratedValue
private Long id;
@CreatedDate
private Instant createdAt;
@LastModifiedDate
private Instant updatedAt;
}Auditing the User
@CreatedBy and @LastModifiedBy record who made the change.
@CreatedBy
private String createdBy;
@LastModifiedBy
private String modifiedBy;AuditorAware
To populate the user fields, provide an AuditorAware bean that returns the current principal.
@Bean
public AuditorAware<String> auditorProvider() {
return () -> Optional.of(
SecurityContextHolder.getContext()
.getAuthentication().getName());
}Reusable Base Class
Put audit fields in a @MappedSuperclass so all entities inherit them.
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Auditable {
@CreatedDate
private Instant createdAt;
@LastModifiedDate
private Instant updatedAt;
}Choosing a Date Type
Audit fields can be Instant, LocalDateTime, or Long. Configure the reference via dateTimeProviderRef if needed.
Verifying Auditing
On the first save createdAt and updatedAt are equal; after an update, only updatedAt changes.
Product p = repository.save(new Product());
// createdAt == updatedAt
p.setName("New");
repository.save(p);
// updatedAt is now later than createdAtQuick Check
Test your understanding of auditing.
Recap
You learned automatic auditing:
@EnableJpaAuditingturns it on@EntityListeners(AuditingEntityListener.class)on entities@CreatedDate/@LastModifiedDatefor timestampsAuditorAwarepopulates@CreatedBy
よくある質問
「@CreatedDateによる監査」レッスンは無料ですか?
はい。「@CreatedDateによる監査」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Boot 4 Microservices & REST APIsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。
「@CreatedDateによる監査」で何を学びますか?
エンティティの作成と変更を自動的に追跡します ブラウザで直接実行するハンズオンコードでSpring Boot 4 Microservices & REST APIsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Spring Boot 4 Microservices & REST APIsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSpring Boot 4 Microservices & REST APIsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「@CreatedDateによる監査」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSpring Boot 4 Microservices & REST APIsレッスンでコードを書いて実行できますか?
はい。すべてのSpring Boot 4 Microservices & REST APIsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Specificationによる動的クエリ
- ProjectionとDTO
- @CreatedDateによる監査
- ページネーションとソート