🎓 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 @Temporal annotation is used in JPA to convert a date and time field in the entity to a SQL type (DATE, TIME, or TIMESTAMP) when mapping to a database. This is crucial for managing how date and time values are persisted and retrieved in applications that use relational databases.
Key Points
1. @Temporal is necessary for fields of type java.util.Date and java.util.Calendar to specify the exact date and time type in the database.
2. Common temporal types include TemporalType.DATE, TemporalType.TIME, and TemporalType.TIMESTAMP.
3. This annotation ensures the correct conversion between Java types and SQL date/time types.
2. Development Steps
1. Define an entity class with java.util.Date or java.util.Calendar fields.
2. Annotate these fields with @Temporal and specify the temporal type.
3. Configure a repository to handle the entity operations.
3. Implementation Example
// Step 1: Define the entity class
import jakarta.persistence.*;
import java.util.Date;
@Entity
public class Event {
@Id
private Long id;
@Temporal(TemporalType.DATE)
@Column(name = "event_date")
private Date eventDate;
// Getters and Setters
}
// Step 2: Configure the repository
import org.springframework.data.jpa.repository.JpaRepository;
public interface EventRepository extends JpaRepository<Event, Long> {
}
Explanation:
1. Event is an entity class where the eventDate field is annotated with @Temporal(TemporalType.DATE). This configuration tells JPA to map the eventDate as a SQL DATE in the database, which includes only the date portion (year, month, day) and omits time.
2. The TemporalType.DATE is used to ensure that no time information is stored in the database for eventDate. If time information were also needed, TemporalType.TIMESTAMP would be used instead.
3. EventRepository extends JpaRepository, providing a way to perform CRUD operations on Event entities. The use of @Temporal in the entity ensures that data integrity is maintained with respect to date handling when entities are persisted and retrieved.
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