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 older javax.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:00
  • ZoneOffset.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) – Once createdAt is 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 than LocalDateTime.


📆 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 createdAt and updatedAt to current UTC time.

@PreUpdate
protected void onUpdate() {
    this.updatedAt = OffsetDateTime.now(ZoneOffset.UTC);
}
  • @PreUpdate – Method called before updating an existing entity.

  • Only updatedAt is 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 too

Standard JavaBeans methods needed for:

  • JPA/Hibernate to access properties.

  • JSON serialization/deserialization (e.g., in REST APIs).


🧠 Summary

ElementPurpose
@MappedSuperclassShare fields with child entities without creating a DB table for it
@IdMarks primary key (id)
UUID.randomUUID()Generates unique ID client-side
@PrePersistSet createdAt, updatedAt on insert
@PreUpdateUpdate updatedAt on update
OffsetDateTimeUTC-safe timestamp (better than LocalDateTime)