Spring Boot & Spring Data JPA - Chapter 1

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/19

flashcard set

Earn XP

Description and Tags

Flashcards covering Spring Boot, JPA entity mapping, repository creation, dependency injection, service methods, controllers, Docker, and application properties.

20 Terms

1
New cards

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;
 }


  1. @Entity (placed on the class itself);

  2. 2. @Id (placed on the id field);

  3. 3. @GeneratedValue(strategy = GenerationType.IDENTITY) (placed on the id field)


2
New cards

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> { }


3
New cards

How do you inject MovieRepository into MovieService using best practices (without using @Autowired)?

Create a constructor:

 public MovieService(MovieRepository repo) { 
        this.movieRepository = repo; 
}


4
New cards

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);

5
New cards

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") );

6
New cards

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.)

7
New cards

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

8
New cards

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

9
New cards

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" }

10
New cards

Write the complete controller method (including its mapping annotation) to fetch ALL movies and return them as a List.

@GetMapping() public List getMovies() { return movieService.getAll(); }

11
New cards

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); }

12
New cards

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);

13
New cards

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;

14
New cards

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).)

15
New cards

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.

16
New cards

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.

17
New cards

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; }

18
New cards

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

19
New cards

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); } }

20
New cards

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); } }