Create Task Entity
Now to create the Task entity!
Define the Instance Variables
Let's create our new Task class in the package we created in the last lesson,
com.devtiro.task.domain.entity.
We'll create this class in steps, starting with the instance variables:
/** Models a task the user plans to do. */
@Entity
@Table(name = "tasks")
public class Task {
/** The task's unique identifier. Generated automatically by JPA. */
@Id
@GeneratedValue(strategy = GenerationType.UUID)
@Column(name = "id", updatable = false, nullable = false)
private UUID id;
/** The title of the task. A maximum length of 255 characters. */
@Column(name = "title", nullable = false)
private String title;
/** A description of the task. A maximum length of 1000 characters. */
@Column(name = "description", length = 1000)
private String description;
/** The date the task is due. */
@Column(name = "due_date")
private LocalDate dueDate;
/** The status of the task: is it open or complete? */
@Enumerated(EnumType.STRING)
@Column(name = "status", nullable = false)
private TaskStatus status;
/** The task's priority: how important is the task - high, medium, or low? */
@Enumerated(EnumType.STRING)
@Column(name = "priority", nullable = false)
private TaskPriority priority;
/** The date and time the task was created. */
@Column(name = "created", nullable = false, updatable = false)
private Instant created;
/** The date and time the task was last updated. */
@Column(name = "updated", nullable = false)
private Instant updated;
}We use the @Entity annotation to mark this as a JPA entity. This class maps
to a database table.
The @Table(name = "tasks") annotation specifies the name of the table
generated by Hibernate DDL Auto.
The @Enumerated(EnumType.STRING) annotation ensures we store enums as strings
in the database, rather than numbers, which is the default. This way they're
easier to read and our business logic won't get confused if the numeric values
change at some point.
Note
Hibernate is the technology our app uses to allow us to interact with the database using Java objects. "Hibernate DDL Auto" is the feature which automatically creates database tables from our entities.
In this app, we're using the default configuration provided by Spring Boot, so when we start our app for the first time, the table is automatically created in the database.
Now we've created the instance variables, let's move on to the constructors.
Generate Constructors
We need two constructors to work with Hibernate. One with no arguments, and one with arguments for all the class's instance variables.
We'll use IntelliJ's code generation feature to create this code for us.
Note
We're not talking about AI code generation here, this feature is a bit simpler than that, but incredibly useful!
Generate a No-Argument Constructor
To generate a constructor with no arguments:
- Right click in the edit window.
- Select "Generate".
- Select "Constructor".
- Ensure no fields are selected.
- Click "OK".
Generate an All-Argument Constructor
To generate a constructor with all arguments:
- Right click in the edit window.
- Select "Generate".
- Select "Constructor".
- Ensure all fields are selected.
- Click "OK".
Note
We can use tools like Project Lombok to generate these constructors for us by using Java annotations. We'll not use Lombok in this project to keep things more approachable, but it's a great tool to know!
Generate Getters and Setters
Now for the getters and setters. Again, we'll use IntelliJ's code generation feature to write this code for us. The IntelliJ default template is absolutely fine for our needs.
- Right click in the edit window.
- Select "Generate" from the menu.
- Select "Getters & Setters".
- Select all the instance variables in the list.
- Click the "OK" button.
Generate Equals and Hashcode
Different equals and hash code strategies exist, with each implementation offering its own benefits and drawbacks.
A deep dive into the pros and cons of each strategy is outside the scope of this build. The short version is our strategy uses only the entity's ID field for its equals and hash code implementation. This approach works perfectly for this build.
- Right click in the edit window.
- Select "Generate" from the menu.
- Select "Equals and Hashcode".
- Select the template "IntelliJ Default".
- Select the
getClasscomparison expression. - Select only the
idfield. - Accept the defaults for the remaining screens. Having
idnullable is fine. - Click the "OK" button.
Generate toString
Finally, let's generate a helpful toString method.
- Right click in the edit window.
- Select "Generate" from the menu.
- Select "toString".
- Select all the instance variables in the list.
- Click the "OK" button.
With this, our Task entity class is now ready to use!
Summary
- Created the
Taskentity.