0
0
Spring Bootframework~10 mins

Service calling repository in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Service calling repository
Client calls Service method
Service method starts
Service calls Repository method
Repository accesses Database
Repository returns data to Service
Service processes data
Service returns result to Client
This flow shows how a client calls a service method, which then calls the repository to get data from the database, processes it, and returns the result.
Execution Sample
Spring Boot
public class UserService {
  private final UserRepository repo;
  public UserService(UserRepository repo) { this.repo = repo; }
  public User getUserById(Long id) {
    return repo.findById(id).orElse(null);
  }
}
A service method calls the repository to find a user by ID and returns the user or null.
Execution Table
StepActionService StateRepository CallRepository ResultService Return
1Client calls getUserById(5)WaitingNo call yetNo resultNo return
2Service calls repo.findById(5)Calling repofindById(5)Searching DBNo return
3Repository queries DB for ID=5Waiting repoDB queryUser{id=5}No return
4Repository returns User to ServiceReceived UserCompletedUser{id=5}No return
5Service returns User to ClientDoneNo callNo resultUser{id=5}
6Client receives UserIdleNo callNo resultUser{id=5}
💡 Execution stops after service returns the user to the client.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
idnull555
repo.findById resultnullPendingUser{id=5}User{id=5}
service returnnullnullUser{id=5}User{id=5}
Key Moments - 2 Insights
Why does the service call the repository instead of accessing the database directly?
The service calls the repository to separate concerns: the repository handles data access, while the service handles business logic. This is shown in steps 2 and 3 where the service calls repo.findById and the repository queries the database.
What happens if the repository does not find a user with the given ID?
If the repository returns empty, the service returns null as shown by orElse(null) in the code. This would be reflected in the execution table by repo.findById result being empty and service return being null.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the repository result at step 3?
AUser{id=5}
BSearching DB
CNo result
DDB query
💡 Hint
Check the 'Repository Result' column at step 3 in the execution_table.
At which step does the service receive the user from the repository?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Service State' and 'Repository Result' columns to find when the service has the user.
If the repository returned empty, what would the service return?
AUser object
Bnull
CException
DEmpty list
💡 Hint
Refer to the code line with orElse(null) in the execution_sample.
Concept Snapshot
Service calls repository to get data.
Repository handles database access.
Service processes and returns data.
Use dependency injection for repository in service.
Service method calls repo method and returns result.
Keep responsibilities separate for clean code.
Full Transcript
In Spring Boot, a service class calls a repository class to get data from the database. The client calls a method on the service. The service method calls the repository method, which queries the database. The repository returns data to the service. The service processes or directly returns this data to the client. This separation keeps code organized and easier to maintain. The example shows a service method getUserById calling repo.findById and returning the user or null if not found.