0% found this document useful (0 votes)
3 views

25. Lesson (Entities and UUID in Spring Boot)

This document explains how to define entities and use UUIDs as primary keys in Spring Boot applications. It covers the creation of entity classes, the use of annotations for mapping, and the implementation of a repository, service, and controller layer. The conclusion emphasizes the benefits of using UUIDs for data consistency and scalability in distributed systems.

Uploaded by

goyalgovind1401
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

25. Lesson (Entities and UUID in Spring Boot)

This document explains how to define entities and use UUIDs as primary keys in Spring Boot applications. It covers the creation of entity classes, the use of annotations for mapping, and the implementation of a repository, service, and controller layer. The conclusion emphasizes the benefits of using UUIDs for data consistency and scalability in distributed systems.

Uploaded by

goyalgovind1401
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

Entities in Spring Boot represent the data that will be persisted to the database. They are typically
mapped to database tables.

Defining an Entity

1. Creating the Entity Class

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;

private String name;


private String email;

// Getters and setters


}

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.

Configuring UUIDs as Primary Keys

1. Update the Entity Class

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;

private String name;


private String email;

// Getters and setters


}

2. Annotations Explained

@GeneratedValue(generator = "UUID") : Specifies that the primary key will be


generated using a UUID generator.
@GenericGenerator(name = "UUID", strategy =
"org.hibernate.id.UUIDGenerator") : Defines a custom generator for UUIDs.

Repository Interface

1. Define the Repository Interface


import org.springframework.data.jpa.repository.JpaRepository;
import java.util.UUID;

public interface UserRepository extends JpaRepository<User, UUID> {


// Custom query methods can be defined here
}

Service Layer

1. Service Class

@Service
public class UserService {
@Autowired
private UserRepository userRepository;

public User createUser(User user) {


return userRepository.save(user);
}

public Optional<User> getUserById(UUID id) {


return userRepository.findById(id);
}

public List<User> getAllUsers() {


return userRepository.findAll();
}

public void deleteUser(UUID id) {


userRepository.deleteById(id);
}
}

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.

You might also like