Spring Data JPA / Create project

If you are here and reading this arcticle, I think you are already aware of what Spring Framework is. If not so, you can check Spring documentations here (I will write my own words about Spring. You can find here in the future)

My main purpose for this article is to describe Spring Data JPA framework. Lets start.

What is Spring Data JPA?

Spring Data JPA library is part of Spring framework which handles RDMS(Relational Database Management Systems). For the ones who does not now about namig convension, it is better to know abbrevations.

Spring: Framework itself
Data: Part of framework which handles data related jobs (RDBMS, No-SQL, etc)
JPA: Java Persistence API

I think it is better to understand the fundementals when we are doing the job. For now, the most important thing to know is being aware of that we are working on a RDMS job.

Create project

First we need to create a project. I have created a new workspace in my STS setup to make clean space. If you do not any project in your workspace you see package explorer screen like following screenshot.

 Click "Create new Spring Starter Project"
Click “Create new Spring Starter Project”
Name the project
Name the project
Select Spring Data JPA and lombok libraries
Select Spring Data JPA and lombok libraries

Check Project Files

Our project was created, now lets check pom.xml file. You can find file using “ctrl+shift+R” in Eclipse/STS

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.enginaar.spring</groupId>
  <artifactId>data</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>data</name>
  <description>Demo project for Spring Boot</description>

  <properties>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

As you see we have our dependencies for spring-boot-starter-data-jpa and lombok. Everything is ok. The next part is DataApplication.java file.

package com.enginaar.spring.data;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DataApplication {

  public static void main(String[] args) {
    SpringApplication.run(DataApplication.class, args);
  }

}

My project has been created with package of com.example.demo. I changed it to com.enginaar.spring.data

Do nothing and just run application as “Spring Boot Application” lets see what’a happening.

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
  If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
  If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

Application start failed. Because this is a Spring Data application, we said that we were going to workn on a database and we do not have any database related configurations. Spring Data JPA uses Hibernate as underlying  technology and it has variety of database suppport. You can check the database list here.

I prefer sqlite when I develop application using Laravel and PHP applications. h2 database is also a good choice for development because of no install required. It’s up to you to decide. For sure you can use any supported one.

I will configure a mariadb. The main reason is that I want to show you a couple of milestones while coding applications. Our application has a properties file named “application.properties” you can find file again using “ctrl+shift+R” in Eclipse/STS

Configure Project

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.datasource.url=jdbc:mysql://localhost:3306/spring
spring.datasource.username=spring
spring.datasource.password=spring
  • The first line says that application will use Mysql Driver as datasource driver.
  • The second line is for hibernate to understand Mysql dialect.
  • The third line is the url for our database.

Now just run the application and we will get another error tells driver not found.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver

The problem is that jre looks for com.mysql.jdbc.Driver class, but it does not exist on our classpath yet.

Add following dependency to pom.xml -> dependencies

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
 </dependency>

Run application again and see that there is no error

Conguragulations!!

We have created a project which has no error. It was very hard in the past to create this application. From here on we will code our application.

See you in next post.