Create Task Entity
Now that we have our basic Spring Boot application set up and connected to PostgreSQL, let's start implementing our domain model, beginning with the Task entity.
First, let's create a new package com.devtiro.tasks.domain.entities to hold our entity classes. In this package, we'll create several files to support our Task entity.
Creating the Task Status Enum
First, let's create a TaskStatus enum to represent the possible states of a task:
public enum TaskStatus {
OPEN,
CLOSED
}Creating the Task Priority Enum
Next, let's create a TaskPriority enum to represent the priority levels:
public enum TaskPriority {
HIGH,
MEDIUM,
LOW
}Creating the Task Entity
Now let's create our main Task class and mark it as a JPA entity:
@Entity
@Table(name = "tasks")
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
@Column(name = "id", updatable = false, nullable = false)
private UUID id;
@Column(name = "title", nullable = false)
private String title;
@Column(name = "description")
private String description;
@Column(name = "due_date")
private LocalDateTime dueDate;
@Column(name = "priority", nullable = false)
private TaskPriority priority;
@Column(name = "status", nullable = false)
private TaskStatus status;
@Column(name = "created", nullable = false)
private LocalDateTime created;
@Column(name = "updated", nullable = false)
private LocalDateTime updated;
}Let's break down the key components:
-
Class Annotations:
@Entitymarks this as a JPA entity@Table(name = "tasks")specifies the database table name
-
ID Field:
- Uses UUID as the primary key type
@GeneratedValuetells JPA to generate IDs automatically- Marked as non-updatable and required with
@Columnattributes
-
Basic Fields:
title: Required field for the task namedescription: Optional field for additional detailsdueDate: Optional field for task deadlinepriority: Required field using our TaskPriority enumstatus: Required field using our TaskStatus enum
-
Audit Fields:
created: Timestamp when the task is createdupdated: Timestamp when the task was last modified
Adding Constructor and Methods
Now let's add constructors and the necessary accessor methods:
// No-args constructor required by JPA
public Task() {
}
// All-args constructor for convenience
public Task(UUID id, String title, String description, LocalDateTime dueDate,
TaskPriority priority, TaskStatus status, LocalDateTime created,
LocalDateTime updated) {
this.id = id;
this.title = title;
this.description = description;
this.dueDate = dueDate;
this.priority = priority;
this.status = status;
this.created = created;
this.updated = updated;
}
// Getters and setters for all fields
// (Use your IDE's generation capability for these)We also need to implement equals(), hashCode(), and toString() methods. Your IDE can generate these for you, making sure to include all fields.
A Note on Date/Time Types
We're using LocalDateTime for our date/time fields because:
- It's the modern Java way to handle dates and times
- It doesn't carry timezone information (which we don't need for this app)
- It's well-supported by both JPA and JSON serialization
Summary
- Created the
Taskentity with all required fields and annotations - Implemented supporting enums for status and priority
- Added proper constructors and accessor methods
- Used UUID for the primary key