Spring Boot 4 Microservices & REST APIs · Lekcja

Audytowanie za pomocą @CreatedDate

Automatycznie śledź utworzenie i zmiany encji

Lekcja 3 z 413 kroki

Audytowanie za pomocą @CreatedDate to bezpłatna lekcja Spring Boot 4 Microservices & REST APIs na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Spring Boot 4 Microservices & REST APIs, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Spring Boot 4 Microservices & REST APIs zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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 createdAt

Quick Check

Test your understanding of auditing.

Recap

You learned automatic auditing:

  • @EnableJpaAuditing turns it on
  • @EntityListeners(AuditingEntityListener.class) on entities
  • @CreatedDate / @LastModifiedDate for timestamps
  • AuditorAware populates @CreatedBy
Bezpłatny start

Ucz się Java dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
24
Lekcje
93

Często zadawane pytania

Czy lekcja „Audytowanie za pomocą @CreatedDate” jest bezpłatna?

Tak — pełny tekst „Audytowanie za pomocą @CreatedDate” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Spring Boot 4 Microservices & REST APIs, przejdź na CoddyKit PRO. Kurs Spring Boot 4 Microservices & REST APIs zawiera 4 lekcji w sumie.

Co nauczysz się w „Audytowanie za pomocą @CreatedDate”?

Automatycznie śledź utworzenie i zmiany encji Ćwiczysz Spring Boot 4 Microservices & REST APIs z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Spring Boot 4 Microservices & REST APIs?

Nie wymagamy żadnego doświadczenia. Spring Boot 4 Microservices & REST APIs w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Audytowanie za pomocą @CreatedDate”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Spring Boot 4 Microservices & REST APIs?

Tak. Każda lekcja Spring Boot 4 Microservices & REST APIs zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Dynamiczne zapytania ze Specifications
  2. Projekcje i DTO
  3. Audytowanie za pomocą @CreatedDate
  4. Stronicowanie i sortowanie
← Powrót do Spring Boot 4 Microservices & REST APIs