Spring Boot 4 Microservices & REST APIs · บทเรียน

การตรวจสอบประวัติด้วย @CreatedDate

ติดตามการสร้างและการเปลี่ยนแปลงเอนทิตีโดยอัตโนมัติ

บทเรียน 3 จาก 413 ขั้นตอน

การตรวจสอบประวัติด้วย @CreatedDate เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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
เริ่มต้นได้ฟรี

เรียนรู้ Java ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
24
บทเรียน
93

คำถามที่พบบ่อย

บทเรียน “การตรวจสอบประวัติด้วย @CreatedDate” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การตรวจสอบประวัติด้วย @CreatedDate” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การตรวจสอบประวัติด้วย @CreatedDate”

ติดตามการสร้างและการเปลี่ยนแปลงเอนทิตีโดยอัตโนมัติ คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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 ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. คำค้นแบบไดนามิกด้วยข้อมูลจำเพาะ
  2. โพรเจกชันและ DTO
  3. การตรวจสอบประวัติด้วย @CreatedDate
  4. การแบ่งหน้าและการเรียงลำดับ
← กลับไปที่ Spring Boot 4 Microservices & REST APIs