0Pricing
Spring Boot 4 Microservices & REST APIs · درس

التدقيق باستخدام @CreatedDate

تتبّع إنشاء الكيانات وتغييراتها تلقائيًا

التدقيق باستخدام @CreatedDate درس مجاني في Spring Boot 4 Microservices & REST APIs على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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 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

الأسئلة الشائعة

هل درس «التدقيق باستخدام @CreatedDate» مجاني؟

نعم — نص درس «التدقيق باستخدام @CreatedDate» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Boot 4 Microservices & REST APIs، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.

ماذا ستتعلم في «التدقيق باستخدام @CreatedDate»؟

تتبّع إنشاء الكيانات وتغييراتها تلقائيًا تتمرن على Spring Boot 4 Microservices & REST APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Spring Boot 4 Microservices & REST APIs؟

لا تُشترط خبرة سابقة. Spring Boot 4 Microservices & REST APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «التدقيق باستخدام @CreatedDate»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Spring Boot 4 Microservices & REST APIs هذا؟

نعم. كل درس في Spring Boot 4 Microservices & REST APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. الاستعلامات الديناميكية باستخدام Specifications
  2. الإسقاطات وDTOs
  3. التدقيق باستخدام @CreatedDate
  4. التقسيم إلى صفحات والفرز
← العودة إلى Spring Boot 4 Microservices & REST APIs