Introduction to database programming with JPA
Who are we?
New to Nerdschool?
meetup.com/bergen-nerdschool/
me-qr.com/qr-code-generator/qr
Join our new Discord!
discord.gg/a9PJqKVx8X
me-qr.com/qr-code-generator/qr
WIFI
Network name: “uib-guest”
Follow the instructions after connecting to the network
Agenda
A simple database schema
id | name | birthday | phone | |
1 | John | 2001-01-03 | 900 12 345 | john@example.com |
2 | Jane | 1980-09-09 | 900 54 321 | jane@example.com |
3 | Paul | 1999-12-30 | 900 66 222 | paul@example.com |
id | license_plate | owner_person_id | model_name | year | color |
1 | R-123 | 1 | Ferrari F-40 | 1985 | red |
2 | O-999 | 2 | Jaguar E-type | 1970 | green |
3 | R-313 | 2 | Belchfire Runabout | 1934 | red |
4 | R-761 | 3 | DMC DeLorean | 1983 | silver |
What does JPA do
What you need to do
A simple Java entity class
class Person {
Long id;
String name;
LocalDate birthday;
String phoneNumber;
String email;
List<Car> cars;
}
class Car {
Long id;
String licensePlate;
Person owner;
String modelName;
Integer year;
String color;
}
Mapping java fields to columns
@Entity
class Person {
@Id Long id;
@Column String name;
@Column LocalDate birthday;
@Column(name = "phone") String phoneNumber;
@Column String email;
List<Car> cars;
}
Mapping relationship between java classes
@Entity
class Person {
@Id Long id;
@Column String name;
@Column LocalDate birthday;
@Column(name = "phone") String phoneNumber;
@Column String email;
@OneToMany(mappedBy = "owner")
List<Car> cars;
}
@Entity
class Car {
@Id Long id;
@Column(name = "license_plate") String licensePlate;
@ManyToOne @JoinColumn(name = "owner_person_id") Person owner;
@Column(name = "model_name") String modelName;
@Column Integer year;
@Column String color;
}
Writing queries
@Repository
public interface MovieRepository extends CrudRepository<Movie, Long> {
@NativeQuery("""
select *
from movies m
where m.date between '1999-03-01' and '1999-03-31'
and m.kind 'movie'
order by m.date desc
""")
List<Movie> findMovies();
}
Using the queries
@Controller
@RequestMapping("/movies")
public class MoviesController {
private final MovieRepository movieRepository;
public MoviesController(MovieRepository movieRepository) {
this.movieRepository = movieRepository;
}
@GetMapping
public ModelAndView list() {
List<Movie> movies = movieRepository.findMovies();
return new ModelAndView("movies/movie-list")
.addObject("movies", movies);
}
}
Selecting columns
SELECT name, phone
FROM person
id | name | birthday | phone | |
1 | John | 2001-01-03 | 900 12 345 | john@example.com |
2 | Jane | 1980-09-09 | 900 54 321 | jane@example.com |
3 | Paul | 1999-12-30 | 900 66 222 | paul@example.com |
Filtering rows
SELECT *
FROM person
WHERE name = 'John'
id | name | birthday | phone | |
1 | John | 2001-01-03 | 900 12 345 | john@example.com |
2 | Jane | 1980-09-09 | 900 54 321 | jane@example.com |
3 | Paul | 1999-12-30 | 900 66 222 | paul@example.com |
Inserting rows
INSERT INTO person (id, name, email)
VALUES (4, 'Trudy', 'trudy@example.com')
id | name | birthday | phone | |
1 | John | 2001-01-03 | 900 12 345 | john@example.com |
2 | Jane | 1980-09-09 | 900 54 321 | jane@example.com |
3 | Paul | 1999-12-30 | 900 66 222 | paul@example.com |
4 | Trudy | null | null | trudy@example.com |
Updating rows
UPDATE person
SET birthday = '2020-01-30'
WHERE id = 4
id | name | birthday | phone | |
1 | John | 2001-01-03 | 900 12 345 | john@example.com |
2 | Jane | 1980-09-09 | 900 54 321 | jane@example.com |
3 | Paul | 1999-12-30 | 900 66 222 | paul@example.com |
4 | Trudy | 2020-01-30 | null | trudy@example.com |
Deleting rows
DELETE FROM person
WHERE id = 4
id | name | birthday | phone | |
1 | John | 2001-01-03 | 900 12 345 | john@example.com |
2 | Jane | 1980-09-09 | 900 54 321 | jane@example.com |
3 | Paul | 1999-12-30 | 900 66 222 | paul@example.com |
4 | Trudy | null | null | trudy@example.com |
Filtering rows by substring
SELECT *
FROM person
WHERE name LIKE 'J%'
id | name | birthday | phone | |
1 | John | 2001-01-03 | 900 12 345 | john@example.com |
2 | Jane | 1980-09-09 | 900 54 321 | jane@example.com |
3 | Paul | 1999-12-30 | 900 66 222 | paul@example.com |
Sorting data
SELECT *
FROM person
ORDER BY birthday DESC
id | name | birthday | phone | |
1 | John | 2001-01-03 | 900 12 345 | john@example.com |
3 | Paul | 1999-12-30 | 900 66 222 | paul@example.com |
2 | Jane | 1980-09-09 | 900 54 321 | jane@example.com |
Using aliases
SELECT p.name
FROM person p
id | name | birthday | phone | |
1 | John | 2001-01-03 | 900 12 345 | john@example.com |
3 | Paul | 1999-12-30 | 900 66 222 | paul@example.com |
2 | Jane | 1980-09-09 | 900 54 321 | jane@example.com |
Joining tables
id | name | birthday | phone | |
1 | John | 2001-01-03 | 900 12 345 | john@example.com |
2 | Jane | 1980-09-09 | 900 54 321 | jane@example.com |
3 | Paul | 1999-12-30 | 900 66 222 | paul@example.com |
id | license_plate | owner_person_id | model_name | year | color |
1 | R-123 | 1 | Ferrari F-40 | 1985 | red |
2 | O-999 | 2 | Jaguar E-type | 1970 | green |
3 | R-313 | 2 | Belchfire Runabout | 1934 | red |
4 | R-761 | 3 | DMC DeLorean | 1983 | silver |
SELECT p.name, c.model_name�FROM person p�INNER JOIN car c ON c.owner_person_id = p.id
id | name | birthday | phone | |
1 | John | 2001-01-03 | 900 12 345 | john@example.com |
2 | Jane | 1980-09-09 | 900 54 321 | jane@example.com |
3 | Paul | 1999-12-30 | 900 66 222 | paul@example.com |
id | license_plate | owner_person_id | model_name | year | color |
1 | R-123 | 1 | Ferrari F-40 | 1985 | red |
2 | O-999 | 2 | Jaguar E-type | 1970 | green |
3 | R-313 | 2 | Belchfire Runabout | 1934 | red |
4 | R-761 | 2 | DMC DeLorean | 1983 | silver |
name | model_name |
John | Ferrari F-40 |
Jane | Jaguar E-type |
Jane | Belchfire Runabout |
Jane | DMC DeLorean |
SELECT p.name, c.model_name�FROM person p�LEFT JOIN car c ON c.owner_person_id = p.id
id | name | birthday | phone | |
1 | John | 2001-01-03 | 900 12 345 | john@example.com |
2 | Jane | 1980-09-09 | 900 54 321 | jane@example.com |
3 | Paul | 1999-12-30 | 900 66 222 | paul@example.com |
id | license_plate | owner_person_id | model_name | year | color |
1 | R-123 | 1 | Ferrari F-40 | 1985 | red |
2 | O-999 | 2 | Jaguar E-type | 1970 | green |
3 | R-313 | 2 | Belchfire Runabout | 1934 | red |
4 | R-761 | 2 | DMC DeLorean | 1983 | silver |
name | model_name |
John | Ferrari F-40 |
Jane | Jaguar E-type |
Jane | Belchfire Runabout |
Jane | DMC DeLorean |
Paul | null |
Assignments
Assignments