Less we dependent on other libraries easier it will be for us to grow
package in.abhi8290.helloworld.core.base;
import jakarta.persistence.*;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.UUID;
@MappedSuperclass
public abstract class BaseEntity {
@Id
private String id = UUID.randomUUID().toString(); // Avoid JPA generation for UUID
@Column(updatable = false, nullable = false)
private OffsetDateTime createdAt;
@Column(nullable = false)
private OffsetDateTime updatedAt;
@PrePersist
protected void onCreate() {
this.createdAt = OffsetDateTime.now(ZoneOffset.UTC);
this.updatedAt = createdAt;
}
@PreUpdate
protected void onUpdate() {
this.updatedAt = OffsetDateTime.now(ZoneOffset.UTC);
}
public String getId() {
return id;
}
public OffsetDateTime getCreatedAt() {
return createdAt;
}
public OffsetDateTime getUpdatedAt() {
return updatedAt;
}
public void setId(String id) {
this.id = id;
}
public void setCreatedAt(OffsetDateTime createdAt) {
this.createdAt = createdAt;
}
public void setUpdatedAt(OffsetDateTime updatedAt) {
this.updatedAt = updatedAt;
}
}Absolutely — let’s go line-by-line and explain everything in your BaseEntity class, especially with respect to Jakarta Persistence (JPA 3.1+).
✅ Complete Breakdown
package in.abhi8290.helloworld.core.base;The Java package where this base class is defined.
import jakarta.persistence.*;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.UUID;Imports:
jakarta.persistence.*– This is the Jakarta EE 9+ namespace, which replaced the olderjavax.persistence.*. It includes annotations like@Id,@MappedSuperclass,@PrePersist, etc.OffsetDateTime– Java 8+ class representing a date-time with a UTC offset (e.g.,2025-05-22T10:20:30+00:00ZoneOffset.UTC– Used to ensure timestamps are saved in UTC.UUID– Generates universally unique identifiers.
🆔 ID Field
@Id
private String id = UUID.randomUUID().toString(); // Avoid JPA generation for UUID-
@Id– Marks this as the primary key. -
UUID generation in Java code (not JPA) – You’re directly assigning a UUID, so you don’t need
@GeneratedValue.
✅ This avoids the need for
GenerationType.UUID, which isn’t supported in all databases or JPA providers.
🕓 Timestamps
@Column(updatable = false, nullable = false)
private OffsetDateTime createdAt;
@Column(nullable = false)
private OffsetDateTime updatedAt;-
@Column(updatable = false)– OncecreatedAtis set, it won’t be modified during updates. -
nullable = false– Field must always have a value in the DB. -
OffsetDateTime– Contains both date-time and UTC offset (e.g.,+00:00), so it’s safer for time zones thanLocalDateTime.
📆 Lifecycle Hooks
@PrePersist
protected void onCreate() {
this.createdAt = OffsetDateTime.now(ZoneOffset.UTC);
this.updatedAt = createdAt;
}-
@PrePersist– Method called before inserting a new entity. -
Sets both
createdAtandupdatedAtto current UTC time.
@PreUpdate
protected void onUpdate() {
this.updatedAt = OffsetDateTime.now(ZoneOffset.UTC);
}-
@PreUpdate– Method called before updating an existing entity. -
Only
updatedAtis refreshed.
These methods ensure automatic timestamping without requiring you to set them manually.
🧾 Getters and Setters
public String getId() { ... }
public OffsetDateTime getCreatedAt() { ... }
public OffsetDateTime getUpdatedAt() { ... }
// Setters tooStandard JavaBeans methods needed for:
-
JPA/Hibernate to access properties.
-
JSON serialization/deserialization (e.g., in REST APIs).
🧠 Summary
| Element | Purpose |
|---|---|
@MappedSuperclass | Share fields with child entities without creating a DB table for it |
@Id | Marks primary key (id) |
UUID.randomUUID() | Generates unique ID client-side |
@PrePersist | Set createdAt, updatedAt on insert |
@PreUpdate | Update updatedAt on update |
OffsetDateTime | UTC-safe timestamp (better than LocalDateTime) |