🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
The @LastModifiedDate annotation is used in Spring Boot to automatically capture the date and time when an entity was last updated. This is useful for auditing purposes and ensures transparency and traceability of modifications in your data.
Key Points
1. @LastModifiedDate marks a field to be automatically updated with the current date and time whenever an entity is updated.
2. Requires JPA auditing to be enabled in your Spring Boot configuration.
3. Works best with a DateTime type field, such as java.util.Date or java.time.LocalDateTime.
2. Development Steps
1. Enable JPA Auditing in the application.
2. Add the @LastModifiedDate annotation to your entity class.
3. Configure a date/time field in your entity to store the last modification date.
3. Implementation
// Step 1: Enable JPA Auditing
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@Configuration
@EnableJpaAuditing
public class AuditConfig {
}
// Step 2: Entity class with @LastModifiedDate
import jakarta.persistence.*;
import org.springframework.data.annotation.LastModifiedDate;
import java.time.LocalDateTime;
@Entity
public class MyEntity {
@Id
private Long id;
@LastModifiedDate
@Column(name = "last_modified_date")
private LocalDateTime lastModifiedDate;
// Getters and Setters
}
// Step 3: Repository for the entity
import org.springframework.data.jpa.repository.JpaRepository;
public interface MyEntityRepository extends JpaRepository<MyEntity, Long> {
}
Explanation:
1. AuditConfig contains the @EnableJpaAuditing annotation, activating Spring Data JPA's auditing capabilities.
2. In MyEntity, the @LastModifiedDate annotation is applied to the lastModifiedDate field. This field is configured to automatically update with the current date and time whenever the entity is modified.
3. The field lastModifiedDate uses LocalDateTime to capture the precise date and time of modification.
4. MyEntityRepository provides the necessary Spring Data repository methods, and due to the auditing configuration, Spring Data JPA handles the automatic update of lastModifiedDate during each save operation.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment