Spring Data JPA Hello World

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Going forward in our series, spring data jpa hello world will provides an example which shows how to insert an entity using spring data jpa.

Spring Data JPA Hello World:

Objective:

We are going to insert an Employee entity using CrudRepository.

Pre-requisites:

 

  1. Create basic spring gradle file.

Go to Spring Initializer and search for “JPA” in dependencies. Generate the file, download and extract it. You will see build.gradle file there. We are going to use mysql database so we need to add mysql dependency. Final build.gradle will be as below.

2. Create a Gradle project in eclipse:

Navigate to File > New > Other > Gradle > Gradle Project and name it SpringDataJPAHelloWorld. 

When you finish, project structure will be as below.

Now, put everything in gradle.build file that you have and paste it into build.gradle file in eclipse.

3. Create application.properties file in src/main/resources.

Now we are going to configure our database details, jdbc and hibernate details in application.properties.

4. Create Employee Entity:

Create Employee entity that we want to persist.

5. Create Employee Repository:

We have used CrudRepository which should provide all default functionality to save and find entities. We need not create classes and override methods.

6. Create Application Class:

 

What have we done in spring data jpa hello world example?

  1. Created Employee Entity
  2. Create a repository which will perform operations on employee entities.
  3. configured mysql and hibernate specific properties in applications.properties file.
  4. Create build.gradle file to provide required dependencies for project.
  5. Create Application class which actually creates and saves employee entities.

Final project structure should look like below.

7. Perform gradle build:

Go to Gradle Tasks tab in eclipse and double click “build”. Your dependencies will be downloaded and project will compile properly. Gradle build will be successful.

 

8. Run Application.java:

Press ctrl+F11, and your main class will run. You will see console output where spring is seen initialised and hibernate queries will be printed. You will have data inserted in database.

I hope this spring data jpa hello world example was useful.

Share Button

1 comment for “Spring Data JPA Hello World

Leave a Reply

Your email address will not be published. Required fields are marked *