1/19
Flashcards covering Spring Boot, JPA entity mapping, repository creation, dependency injection, service methods, controllers, Docker, and application properties.
Given the class below, what three annotations are missing to make it a database table with an auto-incrementing ID?
public class Movie {
private Integer id;
private String title;
}@Entity (placed on the class itself);
2. @Id (placed on the id field);
3. @GeneratedValue(strategy = GenerationType.IDENTITY) (placed on the id field)
Write the complete interface declaration to create a Spring Data repository for a Movie entity that uses an Integer as its primary key.
public interface MovieRepository extends JpaRepository<Integar, Movie> { }How do you inject MovieRepository into MovieService using best practices (without using @Autowired)?
Create a constructor:
public MovieService(MovieRepository repo) {
this.movieRepository = repo;
}You are writing a findMovieById(Integer id) method. Using your repository, write the return statement so it returns the Movie if found, but safely returns null if it doesn't exist.
return movieRepository.findById(id).orElse(null);
Complete this line to replicate your update logic, throwing a NoSuchElementException with the message "Not found" if the ID doesn't exist: Movie existing = repo.findById(id)_____;
.orElseThrow(() -> new NoSuchElementException("Not found") );
After fetching existingInfo from the database and updating its fields using setters, what exact repository method do you call to commit those changes back to the database?
return movieRepository.save(existingInfo); (Note: save() acts as an update when the entity already has an existing ID.)
Given public void deleteMovie(@PathVariable Integer id, @RequestParam String sort), write the exact URL path you would type in an .http file to hit this endpoint for ID 5 and sort desc.
DELETE http://localhost:8080/movies/5?sort=desc
Fill in the blank with the annotation required to map the incoming JSON body from a POST request into the Java object: public Movie addMovie(_____ Movie movie)
@RequestBody
Write the complete .http file syntax to test a PUT request updating a movie with ID 4 at /movies. Provide the HTTP method, URL, Content-Type header, and a JSON body.
PUT http://localhost:8080/movies/4 Content-Type: application/json { "title": "Inception", "genre": "Sci-Fi" }
Write the complete controller method (including its mapping annotation) to fetch ALL movies and return them as a List.
@GetMapping() public List
Write the complete controller method (including mapping annotation and path variable) to fetch a single Movie by its Integer ID.
@GetMapping("/{id}") public Movie findMovieByID(@PathVariable Integer id) { return movieService.findMovieById(id); }
Inside the Service class, you have a method public void deleteMovieById(Integer id). What is the exact repository call to execute this?
movieRepository.deleteById(id);
How do you configure an @Id field to use a custom PostgreSQL sequence named student_sequence that increments by 1?
@Id @SequenceGenerator( name = "student_sequence", sequenceName = "student_sequence", allocationSize = 1 ) @GeneratedValue( strategy = SEQUENCE, generator = "student_sequence" ) private Integer id;
What is the terminal command to run a docker-compose.yml file in detached mode (in the background)?
docker compose up -d (Note: On older versions of Docker, the command is docker-compose up -d (with a hyphen).)
In application.properties, what is the specific purpose of these two properties: spring.datasource.url and spring.datasource.driver-class-name?
url: Defines the connection string to the database (e.g., jdbc:postgresql://localhost:5334/amigos). driver-class-name: Tells Spring which JDBC driver dependency to use (e.g., org.postgresql.Driver) to communicate with that specific database.
In application.properties, what do these two JPA/Hibernate settings do: hibernate.ddl-auto=create-drop and show-sql=true?
create-drop: Tells Hibernate to automatically create your database schema when the app starts, and drop it entirely when the app shuts down (useful for testing). show-sql=true: Logs the raw SQL queries generated by Hibernate to your console.
Write a basic Student entity class mapped to the database. It needs a standard auto-generated id (Integer), a name (String), and an email (String).
@Entity public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; private String email; }
Write the complete interface to create a Spring Data JPA Repository for the Student entity mapped with an Integer primary key.
public interface StudentRepository extends JpaRepository
Write a StudentService class. Inject the StudentRepository via the constructor, and write a method to save a new Student.
@Service public class StudentService { private StudentRepository repo; public StudentService(StudentRepository repo) { this.repo = repo; } public Student save(Student student) { return repo.save(student); } }
Write a StudentController with a base route of /api/students. Create a POST endpoint that receives JSON and saves a Student via the Service.
@RestController @RequestMapping("/api/students") public class StudentController { // constructor injection omitted @PostMapping public Student add(@RequestBody Student s) { return studentService.save(s); } }