Spring Boot 4 Microservices & REST APIs · Lezione

Auditing con @CreatedDate

Tracci automaticamente la creazione e le modifiche delle entità.

Lezione 3 di 413 passaggi

Auditing con @CreatedDate è una lezione Spring Boot 4 Microservices & REST APIs gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Spring Boot 4 Microservices & REST APIs, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Spring Boot 4 Microservices & REST APIs include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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
Gratis per iniziare

Impara Java con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
24
Lezioni
93

Domande Frequenti

La lezione «Auditing con @CreatedDate» è gratuita?

Sì — il testo completo di «Auditing con @CreatedDate» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Spring Boot 4 Microservices & REST APIs, passa a CoddyKit PRO. Il corso Spring Boot 4 Microservices & REST APIs include 4 lezioni in totale.

Cosa imparerò in «Auditing con @CreatedDate»?

Tracci automaticamente la creazione e le modifiche delle entità. Eserciti Spring Boot 4 Microservices & REST APIs con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Spring Boot 4 Microservices & REST APIs?

Non è richiesta alcuna esperienza precedente. Spring Boot 4 Microservices & REST APIs su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.

Quanto tempo richiede la lezione «Auditing con @CreatedDate»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Spring Boot 4 Microservices & REST APIs?

Sì. Ogni lezione Spring Boot 4 Microservices & REST APIs include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Query dinamiche con le Specification
  2. Proiezioni e DTO
  3. Auditing con @CreatedDate
  4. Paginazione e ordinamento
← Torna a Spring Boot 4 Microservices & REST APIs