Auditing mit @CreatedDate
Verfolgen Sie das Erstellen und Ändern von Entitäten automatisch.
Auditing mit @CreatedDate ist eine kostenlose Spring Boot 4 Microservices & REST APIs-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Spring Boot 4 Microservices & REST APIs-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Spring Boot 4 Microservices & REST APIs-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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
Häufig gestellte Fragen
Ist die Lektion „Auditing mit @CreatedDate“ kostenlos?
Ja — der vollständige Text von „Auditing mit @CreatedDate“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Spring Boot 4 Microservices & REST APIs-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Spring Boot 4 Microservices & REST APIs-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Auditing mit @CreatedDate“?
Verfolgen Sie das Erstellen und Ändern von Entitäten automatisch. Du übst Spring Boot 4 Microservices & REST APIs mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Spring Boot 4 Microservices & REST APIs zu starten?
Keine Vorkenntnisse erforderlich. Spring Boot 4 Microservices & REST APIs auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „Auditing mit @CreatedDate“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Spring Boot 4 Microservices & REST APIs-Lektion Code schreiben und ausführen?
Ja. Jede Spring Boot 4 Microservices & REST APIs-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Dynamische Abfragen mit Specifications
- Projections und DTOs
- Auditing mit @CreatedDate
- Seitennummerierung und Sortierung