Skip to content

Latest commit

 

History

History
45 lines (26 loc) · 2.15 KB

File metadata and controls

45 lines (26 loc) · 2.15 KB

Exercise 6 - Querying Movies with JPA

In this exercise, we'll switch from writing raw SQL to using the Java Persistence API (JPA) with Spring Boot. We'll run a web application that queries the same Postgres database and displays results in a browser.

You will learn to:

  • Run a Spring Boot application that connects to PostgreSQL
  • Modify a JPA repository query
  • Add a new field to a JPA entity

6.1 Running the project

❗ Make sure the Postgres Docker container from exercise 1 is still running before proceeding.

✏️ In a new terminal, navigate to the repository directory and run:

mvn clean spring-boot:run

✏️ Visit http://localhost:8080 in your browser. You should see a list of movies released in March 1999.

📖 This list is generated by the MoviesController class, which uses MovieRepository to query the database. After you make changes to the code, you must restart the project. If you use IntelliJ, simply rebuilding the project (Build -> Build Project) will trigger an automatic reload.

6.2 List all the Star Wars movies

📖 The MovieRepository.findMovies() method currently returns movies from March 1999. We want it to return Star Wars movies instead.

✏️ Change the method MovieRepository.findMovies() so that it returns all the Star Wars movies.

💡 Hint

You can use the LIKE keyword and the % wildcard to match parts of a string. Note that LIKE is case sensitive. There is a Postgres specific keyword called ILIKE which does a case-insensitive comparison. This is not part of the SQL standard, so it is not supported by all databases. The standard approach is to use lower(m.name) LIKE '%something%'.

6.3 Link to homepage

📖 In the movies table, you'll find a column called homepage, but there is no corresponding field in the Movie java class.

✏️ Add a homepage field to the Movie java class and display a link to the homepage of the movie in movie-list.jsp.

💡 Not all movies have a homepage — make sure the link doesn't break for movies without one.