Database and app orchestration helps your app talk to the database smoothly. It keeps data safe and organized while your app runs.
Database and app orchestration in Spring Boot
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository<User, Long> { // Custom database queries can go here }
This example shows how to create a repository interface to connect your app with the database.
Spring Boot uses this interface to handle database operations automatically.
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface ProductRepository extends JpaRepository<Product, Integer> { // Methods to find products can be added here }
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface OrderRepository extends JpaRepository<Order, Long> { // Custom queries for orders }
This Spring Boot app defines a User entity and a UserRepository to handle database actions. The UserController fetches all users from the database when you visit '/users'.
package com.example.demo; import jakarta.persistence.Entity; import jakarta.persistence.Id; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } @Entity class User { @Id private Long id; private String name; public User() {} public User(Long id, String name) { this.id = id; this.name = name; } public Long getId() { return id; } public String getName() { return name; } } @Repository interface UserRepository extends JpaRepository<User, Long> {} @RestController class UserController { private final UserRepository userRepository; public UserController(UserRepository userRepository) { this.userRepository = userRepository; } @GetMapping("/users") public List<User> getUsers() { return userRepository.findAll(); } }
Spring Boot automatically manages database connections for you.
Use @Entity to mark classes that map to database tables.
Repositories extend JpaRepository to get many database methods without writing code.
Database and app orchestration connects your app to the database smoothly.
Spring Boot uses repositories and entities to manage data easily.
This helps your app save, read, update, and delete data safely.
Practice
Solution
Step 1: Understand repository purpose
Spring Boot repositories act as a bridge between the app and the database, offering easy data operations.Step 2: Identify repository capabilities
They provide built-in methods like save(), findById(), delete(), which handle data safely without manual SQL.Final Answer:
To provide methods for saving, reading, updating, and deleting data -> Option AQuick Check:
Repository = Data operations [OK]
- Confusing repositories with UI components
- Thinking repositories define database schema
- Assuming repositories handle security
User with primary key type Long?Solution
Step 1: Recall correct interface extension
Spring Data JPA repositories extend JpaRepository to get full CRUD support.Step 2: Check syntax correctness
public interface UserRepository extends JpaRepository {} correctly extends JpaRepository with User entity and Long ID type; others misuse implements or wrong generics order.Final Answer:
public interface UserRepository extends JpaRepository {} -> Option BQuick Check:
JpaRepository syntax [OK]
- Using implements instead of extends
- Swapping generic type order
- Extending incomplete Repository interface
Optional<User> user = userRepository.findById(5L);
What will
user.isPresent() return if no user with ID 5 exists in the database?Solution
Step 1: Understand findById return type
findById returns an Optional which is empty if no matching record is found.Step 2: Check isPresent behavior
isPresent() returns false when Optional is empty, meaning no user found.Final Answer:
false -> Option DQuick Check:
Optional empty means isPresent() = false [OK]
- Assuming isPresent() returns true always
- Expecting null instead of Optional
- Thinking it throws exception if not found
public interface ProductRepository extends JpaRepository {
List<Product> findByName(String name);
}Which of the following issues will cause a runtime error when calling
findByName(null)?Solution
Step 1: Analyze null parameter in derived queries
Spring Data JPA derived query methods accept null parameters and generate SQL like WHERE name = null, which matches no rows (since NULL = NULL is false).Step 2: Confirm behavior
No exception is thrown at runtime or compile time; it safely returns an empty list.Final Answer:
No error; it returns an empty list -> Option AQuick Check:
findByName(null) = empty list [OK]
- Thinking null causes NullPointerException
- Believing signature won't compile
- Assuming null returns all products
1. Retrieve user by ID
2. Change user's email
3. Save updated user back to database
Which code snippet correctly implements this?
Solution
Step 1: Retrieve user safely
Use findById(id).orElseThrow() to get user or throw if missing.Step 2: Modify user object
Set new email on retrieved user object.Step 3: Save updated user
Call save(user) to persist changes back to database.Final Answer:
var user = userRepository.findById(id).orElseThrow(); user.setEmail(newEmail); userRepository.save(user); -> Option CQuick Check:
Retrieve, modify, save = update pattern [OK]
- Trying to save new object without ID
- Using non-existent updateEmailById method
- Calling getEmail() instead of setEmail()
Ready to prove it?
Pass the test and this pattern moves unknown
