Create the Post Entity

In our previous lessons, we created the foundational entities for users, categories, and tags.

Now we'll implement the Post entity and its associated PostStatus enum, which together represent the actual blog content in our system.

These classes will enable us to manage blog posts throughout their lifecycle, from draft to publication.

Understanding Post Status

A post status enum helps track the editorial state of each blog post in our system.

Let's create the PostStatus enum in com.devtiro.blog.domain:

package com.devtiro.blog.domain; public enum PostStatus { DRAFT, PUBLISHED }

Implementing the Post Entity

The Post entity represents individual blog entries and must store all content and metadata.

Here's our initial implementation:

package com.devtiro.blog.domain.entities; import com.devtiro.blog.PostStatus; import jakarta.persistence.*; import lombok.*; import java.time.LocalDateTime; import java.util.Objects; import java.util.UUID; @Entity @NoArgsConstructor @AllArgsConstructor @Getter @Setter @Builder @Table(name = "posts") public class Post { @Id @GeneratedValue(strategy = GenerationType.UUID) private UUID id; @Column(nullable = false) private String title; @Column(nullable = false, columnDefinition = "TEXT") private String content; @Column(nullable = false) @Enumerated(EnumType.STRING) private PostStatus status; @Column(nullable = false) private Integer readingTime; @Column(nullable = false) private LocalDateTime createdAt; @Column(nullable = false) private LocalDateTime updatedAt; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Post post = (Post) o; return Objects.equals(id, post.id) && Objects.equals(title, post.title) && Objects.equals(content, post.content) && status == post.status && Objects.equals(readingTime, post.readingTime) && Objects.equals(createdAt, post.createdAt) && Objects.equals(updatedAt, post.updatedAt); } @Override public int hashCode() { return Objects.hash(id, title, content, status, readingTime, createdAt, updatedAt); } @PrePersist protected void onCreate() { createdAt = LocalDateTime.now(); updatedAt = LocalDateTime.now(); } @PreUpdate protected void onUpdate() { updatedAt = LocalDateTime.now(); } }

Entity Design Considerations

The columnDefinition = "TEXT" annotation ensures our database can store long-form blog content without size restrictions.

The @Enumerated(EnumType.STRING) annotation stores the post status as a readable string rather than a numeric value.

We include a readingTime field to enhance the user experience by showing estimated reading duration.

Audit Fields Implementation

The createdAt and updatedAt fields track the post's lifecycle.

JPA's @PrePersist and @PreUpdate annotations automatically maintain these timestamps.

This audit trail is valuable for content management and debugging.

Summary

  • The PostStatus enum provides a type-safe way to track editorial state
  • Post content uses the TEXT column type to handle varying content lengths
  • Automatic timestamp management ensures accurate audit trails
  • Reading time estimation enhances the user experience
  • Custom equals and hashCode methods maintain entity integrity
© 2026 Devtiro Ltd. All rights reserved