25. Lesson (Entities and UUID in Spring Boot)
25. Lesson (Entities and UUID in Spring Boot)
Entities and UUIDs are fundamental concepts in Spring Boot applications, especially when dealing
with persistence and data consistency. In this article, we will discuss how to define entities and use
UUIDs as primary keys in a Spring Boot application.
Entities in Spring Boot represent the data that will be persisted to the database. They are typically
mapped to database tables.
Defining an Entity
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
2. Annotations Explained
@Entity : Specifies that the class is an entity and is mapped to a database table.
@Table : Specifies the name of the database table to be used for mapping.
@Id : Specifies the primary key of an entity.
@GeneratedValue : Specifies the generation strategy for the primary key.
Using UUIDs as Primary Keys
UUIDs (Universally Unique Identifiers) are a good choice for primary keys because they are unique
across different systems and ensure data consistency. Using UUIDs can be particularly useful in
microservices and distributed systems where unique identification is crucial.
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.UUID;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator"
private UUID id;
2. Annotations Explained
Repository Interface
Service Layer
1. Service Class
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
Controller Layer
1. Controller Class
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@PostMapping
public ResponseEntity<User> createUser(@RequestBody User user) {
User createdUser = userService.createUser(user);
return new ResponseEntity<>(createdUser, HttpStatus.CREATED);
}
@GetMapping("/{id}")
public ResponseEntity<User> getUserById(@PathVariable UUID id) {
Optional<User> user = userService.getUserById(id);
return user.map(ResponseEntity::ok).orElseGet(() -> new ResponseEntity<>
}
@GetMapping
public ResponseEntity<List<User>> getAllUsers() {
List<User> users = userService.getAllUsers();
return new ResponseEntity<>(users, HttpStatus.OK);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteUser(@PathVariable UUID id) {
userService.deleteUser(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
Conclusion
Using UUIDs as primary keys in Spring Boot entities provides a robust and unique way to identify
records, which is especially useful in distributed systems. By following the guidelines and examples
provided, you can effectively implement entities with UUIDs in your Spring Boot application,
ensuring data consistency and scalability.