In this article, we will use the JPA (Java Persistence API) with Spring Boot for insert, update, delete and read operations.

We are using in-memory H2 database, but the same can be changed for any other relational database.

Project Structure

We can use Spring Tool Suite(STS) to create a Spring starter project and choose the h2 and jpa dependencies.

We can also use Spring Initializr for the same (https://start.spring.io/).

pom.xml

We need to add the spring-boot-starter-data-jpa and the h2 dependencies for connecting h2database using JPA.

1

2

3

4

5

6

7

8

9

10

11

12

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

<dependency>

<groupId>com.h2database</groupId>

<artifactId>h2</artifactId>

<scope>runtime</scope>

</dependency>

Entity

The following Employee entity class uses JPA annotations @Entity from javax.persistence package. This @Entity annotation marks the class as Entity to be mapped to a database table.

the Long id property is marked with annotations @Id and @GeneratedValue. Therefore, id will be the primary key of the table and will be autogenerated.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

packagecom.topjavatutorial.entity;

importjavax.persistence.Entity;

importjavax.persistence.GeneratedValue;

importjavax.persistence.GenerationType;

importjavax.persistence.Id;

@Entity

publicclassEmployee{

@Id

@GeneratedValue(strategy=GenerationType.AUTO)

Longid;

Stringname;

intage;

publicEmployee(Stringname,intage){

this.name=name;

this.age=age;

}

publicEmployee(){

}

publicLonggetId(){

returnid;

}

publicvoidsetId(Longid){

this.id=id;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicintgetAge(){

returnage;

}

publicvoidsetAge(intage){

this.age=age;

}

publicStringtoString(){

StringBuffer sb=newStringBuffer("Employee(");

sb.append("Id : ");

sb.append(id);

sb.append(", Name : ");

sb.append(name);

sb.append(", Age : ");

sb.append(age);

sb.append(")");

returnsb.toString();

}

}

Service

We will be using the following EmployeeService class(annotated with @Service) to interact with the Employee table.

It has operations for adding, updating, deleting and reading employee records.

This class has an autowired EmployeeRepository instance through which we will performing database interactions.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

packagecom.topjavatutorial.service;

importjava.util.List;

importorg.slf4j.Logger;

importorg.slf4j.LoggerFactory;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.stereotype.Service;

importcom.topjavatutorial.entity.Employee;

importcom.topjavatutorial.repo.EmployeeRepository;

@Service

publicclassEmployeeService{

privatestaticfinalLogger log=LoggerFactory

.getLogger(EmployeeService.class);

@Autowired

EmployeeRepository empRepo;

publicList<Employee>getAllEmployees(){

log.info("inside EmployeeService.getAllEmployees");

returnempRepo.findAll();

}

publicEmployee getEmployeeById(intid){

log.info("Getting employee details for ID "+id);

returnempRepo.findOne(newLong(id));

}

publicvoidaddEmployee(Employee employee){

log.info("Adding Employee "+employee);

empRepo.save(employee);

}

publicvoidupdateEmployee(Employee employee){

log.info("Updating Employee "+employee);

empRepo.saveAndFlush(employee);

}

publicvoiddeleteEmployee(intid){

log.info("Deleting employee for ID "+id);

empRepo.delete(newLong(id));

}

}

Repository

Here is the code for EmployeeRepository interface that we @Autowired in the service class.

This interface extends a JpaRepository interface that uses generics and need an entity class marked with @Entity with a property marked with @Id annotation. For our example, the entity class is Employee and id is of type Long.

JpaRepository in turn expends PagingAndSortingRepository interface that provides the methods save(), findAll(), delete() that we used with the EmployeeRepository instance in the service class.