About Mary Zheng

Mary has graduated from Mechanical Engineering department at ShangHai JiaoTong University. She also holds a Master degree in Computer Science from Webster University. During her studies she has been involved with a large number of projects ranging from programming and software engineering. She works as a senior Software Engineer in the telecommunications sector where she acts as a leader and works with others to design, implement, and monitor the software solution.

In this example, I will demonstrate how to use Spring Data JpaRepository to create, update, delete, and search contact data in a relational database.

2. Technologies Used

The example code in this article was built and run using:

Java 1.8.101 (1.8.x will do fine)

Maven 3.3.9 (3.3.x will do fine)

Eclipse Mars (Any Java IDE would work)

Spring data 1.5.10.RELEASE

Hibernate 5.0.12.Final

H2 1.4.196

3. Spring Boot JPA Project

Spring Boot Starters provides more than 30 starters to ease the dependency management for your project. The easiest way to generate a Spring Boot JPA with Hibernate project is via Spring starter tool with the steps below:

3.2 JPA Demo Application

The generated JpaDemoApplication.java is annotated with @SpringBootApplication. It is equivalent to use @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes.

We will annotate it with @EnableJpaRepositories. It enables Spring to scan the base package and all its sub-packages for any interfaces extending Repository or one of its sub-interfaces. For each interface found, Spring creates the appropriate bean to handle invocation of the query methods.

We will configure several Spring Beans to manage transactions in the H2 datasource.

4.2 Contact Note

The Contact_Note entity class is mapped to Contact_Note table in the database. Each contact can have many contact notes.

Contact_Note.java

01

02

03

04

05

06

07

08

09

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

packagejcg.zheng.demo.jpademo.entity;

importjavax.persistence.Entity;

importjavax.persistence.GeneratedValue;

importjavax.persistence.Id;

importjavax.persistence.JoinColumn;

importjavax.persistence.ManyToOne;

@Entity

publicclassContact_Note {

@Id

@GeneratedValue

privateLong id;

privateString message;

@ManyToOne

@JoinColumn(name = "CONTACT_ID")

privateContact contact;

publicContact_Note() {

super();

}

publicLong getId() {

returnid;

}

publicvoidsetId(Long id) {

this.id = id;

}

publicContact getContact() {

returncontact;

}

publicvoidsetContact(Contact contact) {

this.contact = contact;

}

publicString getMessage() {

returnmessage;

}

publicvoidsetMessage(String message) {

this.message = message;

}

}

4.3 Phone Type

Phone type is defined as an enum and is mapped to the Phone_type column in the CONTACT table.

PhoneType.java

1

2

3

4

5

packagejcg.zheng.demo.jpademo.type;

publicenumPhoneType {

OFFICE, HOME, MOBILE, OTHER;

}

5. ContactRepository

The repository interface must extend Repository and be typed to the domain class and an ID type. In this step, we will annotate ContactRepository with @Repository and extend it from JpaRepository along with the Contact domain and its ID type. It serves two purposes:

By extending JpaRepository we get a bunch of generic CRUD methods to create, update, delete, and find contacts.

It allows Spring to scan the classpath for this interface and create a Spring bean for it.

Hibernate: selectcontact0_.ID as ID1_0_, contact0_.Email as Email2_0_, contact0_.First_Name as First_Na3_0_, contact0_.Last_Name as Last_Nam4_0_, contact0_.Phone_Number as Phone_Nu5_0_, contact0_.Phone_Type as Phone_Ty6_0_ from CONTACT contact0_ where contact0_.Phone_Type=? and contact0_.Last_Name=?

7. Spring Data JPARepository – Summary

In this example, we built a Spring Boot project to demonstrate how easy it is to configure and use Spring data JpaRepository to manage the data in the database.

Spring Data JPA provides an abstraction to the persistence layer and has a common API. All you have to do is to implement a Repository interface, declare the repository methods and annotate the methods with @Query that should be executed.

However, There are some pitfalls when using JPA. Please check here for detail.

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.