1 of 29

Introduction to database programming with JPA

2 of 29

Who are we?

Even Ask Sleire

Consultant at Kantegaeven.sleire@kantega.no

Erlend Røsjø

Solutions Architect @ Sparebanken Norge�erlend.rosjo@nerdschool.no

3 of 29

4 of 29

New to Nerdschool?

  • Nerdschool is a series of workshop exercises teaching tools and techniques commonly used in real software development
  • All organizers are professional developers
  • We aim for 12 - 14 workshops over a year - 6 - 7 per school semester, every two weeks
  • Next workshop: "Fra design til utvikling" (21.04.26)

5 of 29

meetup.com/bergen-nerdschool/

me-qr.com/qr-code-generator/qr

6 of 29

Join our new Discord!

discord.gg/a9PJqKVx8X

me-qr.com/qr-code-generator/qr

7 of 29

WIFI

Network name: “uib-guest”

Follow the instructions after connecting to the network

8 of 29

Agenda

  • Short introduction to database-tables
  • What is JPA?
  • How to use it
  • Assignments in github

9 of 29

A simple database schema

id

name

birthday

phone

email

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

10 of 29

What does JPA do

  • Connect to database
  • Fetch rows from database tables and create java objects
  • Insert, update or delete rows in database tables based on those java objects

11 of 29

What you need to do

  • Create Java classes that represent tables
  • Describe which fields in the Java classes correspond to which columns in the database tables
  • Write queries that fetch data from the database

12 of 29

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;

}

13 of 29

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;

}

14 of 29

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;

}

15 of 29

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

}

16 of 29

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

}

}

17 of 29

Selecting columns

SELECT name, phone

FROM person

id

name

birthday

phone

email

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

18 of 29

Filtering rows

SELECT *

FROM person

WHERE name = 'John'

id

name

birthday

phone

email

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

19 of 29

Inserting rows

INSERT INTO person (id, name, email)

VALUES (4, 'Trudy', 'trudy@example.com')

id

name

birthday

phone

email

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

20 of 29

Updating rows

UPDATE person

SET birthday = '2020-01-30'

WHERE id = 4

id

name

birthday

phone

email

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

21 of 29

Deleting rows

DELETE FROM person

WHERE id = 4

id

name

birthday

phone

email

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

22 of 29

Filtering rows by substring

SELECT *

FROM person

WHERE name LIKE 'J%'

id

name

birthday

phone

email

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

23 of 29

Sorting data

SELECT *

FROM person

ORDER BY birthday DESC

id

name

birthday

phone

email

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

24 of 29

Using aliases

SELECT p.name

FROM person p

id

name

birthday

phone

email

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

25 of 29

Joining tables

id

name

birthday

phone

email

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

26 of 29

SELECT p.name, c.model_name�FROM person p�INNER JOIN car c ON c.owner_person_id = p.id

id

name

birthday

phone

email

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

27 of 29

SELECT p.name, c.model_name�FROM person p�LEFT JOIN car c ON c.owner_person_id = p.id

id

name

birthday

phone

email

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

28 of 29

Assignments

29 of 29

Assignments

  • Go to: https://github.com/nerdschoolbergen/databases
  • Follow the instructions in README.md