Spring Boot @Enumerated Annotation

1. Introduction

When modeling entities in Spring Boot applications that need to interact with a relational database using JPA, you often encounter the need to persist Enum types. The @Enumerated annotation is a JPA feature that allows you to map an Enum to a database column, ensuring that the data integrity and the representation of enum values are preserved in the database. In this post, we'll explore how to use the @Enumerated annotation within a Spring Boot application.

Key Points:

1. @Enumerated is used to persist Enum types in JPA entities.

2. It has two strategies for storing Enum values: EnumType.ORDINAL and EnumType.STRING.

3. EnumType.ORDINAL stores the Enum values as integer indexes representing their positions in the Enum declaration.

4. EnumType.STRING stores the Enum values as the name of the Enum constants.

5. It is recommended to use EnumType.STRING for better clarity and to avoid issues if the order of the Enum constants changes.

2. Implementation Steps

1. Add the necessary JPA dependency (like spring-boot-starter-data-jpa) to your project.

2. Create an Enum type that you want to persist.

3. Define a JPA Entity class that includes a field of this Enum type.

4. Annotate the Enum field in the entity with @Enumerated and choose the appropriate EnumType.

5. Implement a repository interface to allow CRUD operations on the entity.

6. Use the repository to persist the entity and retrieve it from the database to observe the Enum mapping.

3. Implementation Example

// Step 1: Define the Enum type
public enum Status {
    OPEN, IN_PROGRESS, CLOSED;
}

// Step 2: Define the JPA Entity class with an Enum field
@Entity
public class Task {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String description;

    // Step 3: Use @Enumerated to specify how the Enum should be persisted
    @Enumerated(EnumType.STRING)
    private Status status;

    // Standard getters and setters
}

// Step 4: Create a repository interface
public interface TaskRepository extends JpaRepository<Task, Long> {
}

// Step 5: Use the repository in a service class
@Service
public class TaskService {
    private final TaskRepository taskRepository;

    @Autowired
    public TaskService(TaskRepository taskRepository) {
        this.taskRepository = taskRepository;
    }

    public Task createTask(String description, Status status) {
        Task task = new Task();
        task.setDescription(description);
        task.setStatus(status);
        return taskRepository.save(task);
    }

    // Additional service methods
}

// Step 6: Optionally, create a REST Controller to expose the task operations
@RestController
@RequestMapping("/api/tasks")
public class TaskController {
    private final TaskService taskService;

    @Autowired
    public TaskController(TaskService taskService) {
        this.taskService = taskService;
    }

    @PostMapping
    public Task createTask(@RequestBody Task task) {
        return taskService.createTask(task.getDescription(), task.getStatus());
    }
}

// Step 7: Run the application
@SpringBootApplication
public class EnumeratedExampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(EnumeratedExampleApplication.class, args);
    }
}

// After running the application, you can create a new task via the REST endpoint.

Output:

// The POST request to /api/tasks with JSON payload {"description": "New Task", "status": "OPEN"} will yield:
{
    "id": 1,
    "description": "New Task",
    "status": "OPEN"
}
// The Status enum is stored as a string in the database.

Explanation:

1. Status: An Enum that defines the possible statuses for a task.

2. @Entity and @Id: Annotations that indicate Task is a JPA entity and id is its primary key.

3. @Enumerated(EnumType.STRING): Indicates that the status field should be persisted as a string in the database, storing the name of the enum constant.

4. TaskRepository: A repository interface extending JpaRepository, which provides CRUD operations for Task entities.

5. TaskService: A service class that uses TaskRepository to handle the logic of creating tasks.

6. @Service: Indicates that TaskService is a Spring-managed service bean.

7. TaskController: A REST controller that handles incoming HTTP requests to perform operations on tasks.

8. @RestController and @RequestMapping: Annotations to define a REST controller and map web requests to handler methods.

9. @PostMapping: Maps the HTTP POST requests to the createTask method in TaskController.

10. @RequestBody: Indicates that a method parameter should be bound to the body of the web request.

11. EnumeratedExampleApplication: The main Spring Boot application class that enables auto-configuration and component scanning.

12. SpringApplication.run(): Boots up the Spring Boot application, setting up the JPA entity manager and the web context.

Comments