• Java
    • JAXB Tutorial
      • What is JAXB
      • JAXB Marshalling Example
      • JAXB UnMarshalling Example
  • Spring Tutorial
    • Spring Core Tutorial
    • Spring MVC Tutorial
      • Quick Start
        • Flow Diagram
        • Hello World Example
        • Form Handling Example
      • Handler Mapping
        • BeanNameUrlHandlerMapping
        • ControllerClassNameHandlerMapping
        • SimpleUrlHandlerMapping
      • Validation & Exception Handling
        • Validation+Annotations
        • Validation+ResourceBundle
        • @ExceptionHandler
        • @ControllerAdvice
        • Custom Exception Handling
      • Form Tag Library
        • Textbox Example
        • TextArea Example
        • Password Example
        • Dropdown Box Example
        • Checkboxes Example
        • Radiobuttons Example
        • HiddenValue Example
      • Misc
        • Change Config file name
    • Spring Boot Tutorial
  • Hibernate Tutorial
  • REST Tutorial
    • JAX-RS REST @PathParam Example
    • JAX-RS REST @QueryParam Example
    • JAX-RS REST @DefaultValue Example
    • JAX-RS REST @Context Example
    • JAX-RS REST @MatrixParam Example
    • JAX-RS REST @FormParam Example
    • JAX-RS REST @Produces Example
    • JAX-RS REST @Consumes Example
    • JAX-RS REST @Produces both XML and JSON Example
    • JAX-RS REST @Consumes both XML and JSON Example
  • Miscellaneous
    • JSON Parser
      • Read a JSON file
      • Write JSON object to File
      • Read / Write JSON using GSON
      • Java Object to JSON using JAXB
    • CSV Parser
      • Read / Write CSV file
      • Read/Parse/Write CSV File – OpenCSV
      • Export data into a CSV File
      • CsvToBean and BeanToCsv – OpenCSV

JavaInterviewPoint

Java Development Tutorials

Spring Data JPA CRUD Example using JpaRepository

August 28, 2017 by javainterviewpoint 1 Comment

In this article, we will learn how to integrate Spring Data JPA into our Spring application. We will be extending JPARepository and be creating an Employee management application and store the details using Oracle database.

Creating table

Create EMPLOYEE Table, simply Copy and Paste the following SQL query in the query editor to get the table created.

CREATE TABLE "EMPLOYEE" 
 ( 
    "ID" NUMBER(10) NOT NULL ENABLE, 
    "AGE" NUMBER(10), 
    "DEPT" VARCHAR2(255 CHAR), 
    "NAME" VARCHAR2(255 CHAR), 
     PRIMARY KEY ("ID")
 );

Folder Structure:

Spring Data JPA CRUD Example

  1. Create a simple Maven Project “SpringDataJPA” by selecting maven-archetype-quickstart  and create a package for our source files “com.javainterviewpoint” under  src/main/java 
  2. Now add the following dependency in the POM.xml
    <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	http://maven.apache.org/maven-v4_0_0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<groupId>com.javainterviewpoint</groupId>
    	<artifactId>SpringJPA</artifactId>
    	<packaging>jar</packaging>
    	<version>0.0.1-SNAPSHOT</version>
    	<name>SpringJPA Maven Webapp</name>
    	<url>http://maven.apache.org</url>
    	<properties>
    		<hibernate.version>4.2.0.Final</hibernate.version>
    		<mysql.connector.version>5.1.21</mysql.connector.version>
    		<spring.version>4.3.5 RELEASE</spring.version>
    	</properties>
    
    
    	<dependencies>
    		<dependency>
    			<groupId>cglib</groupId>
    			<artifactId>cglib</artifactId>
    			<version>${cglib.version}</version>
    		</dependency>
    
    		<!-- DB related dependencies -->
    
    		<dependency>
    			<groupId>org.hibernate.common</groupId>
    			<artifactId>hibernate-commons-annotations</artifactId>
    			<version>4.0.4.Final</version>
    		</dependency>
    		<dependency>
    			<groupId>commons-dbcp</groupId>
    			<artifactId>commons-dbcp</artifactId>
    			<version>1.4</version>
    		</dependency>
    		<dependency>
    			<groupId>javassist</groupId>
    			<artifactId>javassist</artifactId>
    			<version>3.12.1.GA</version>
    		</dependency>
    		<dependency>
    			<groupId>org.hibernate.javax.persistence</groupId>
    			<artifactId>hibernate-jpa-2.0-api</artifactId>
    			<version>1.0.1.Final</version>
    		</dependency>
    		<!-- SPRING -->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-webmvc</artifactId>
    			<version>4.3.5.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-core</artifactId>
    			<version>4.3.5.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-tx</artifactId>
    			<version>4.3.5.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-jdbc</artifactId>
    			<version>4.3.5.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-orm</artifactId>
    			<version>3.2.5.RELEASE</version>
    		</dependency>
    		<!-- CGLIB is required to process @Configuration classes -->
    		<dependency>
    			<groupId>cglib</groupId>
    			<artifactId>cglib</artifactId>
    			<version>2.2.2</version>
    		</dependency>
    		<!-- Servlet API and JSTL -->
    		<dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>javax.servlet-api</artifactId>
    			<version>3.0.1</version>
    			<scope>provided</scope>
    		</dependency>
    		<dependency>
    			<groupId>jstl</groupId>
    			<artifactId>jstl</artifactId>
    			<version>1.2</version>
    		</dependency>
    
    		<!-- Test -->
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.7</version>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-test</artifactId>
    			<version>4.3.5.RELEASE</version>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-test-mvc</artifactId>
    			<version>1.0.0.M1</version>
    			<scope>test</scope>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
    		<dependency>
    			<groupId>org.springframework.data</groupId>
    			<artifactId>spring-data-jpa</artifactId>
    			<version>1.11.3.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>com.oracle</groupId>
    			<artifactId>ojdbc14</artifactId>
    			<version>11.2.0</version>
    		</dependency>
    		<dependency>
    			<groupId>org.hibernate</groupId>
    			<artifactId>hibernate-core</artifactId>
    			<version>4.1.9.Final</version>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-tx</artifactId>
    			<version>4.3.5.RELEASE</version>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context</artifactId>
    			<version>4.3.5.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.hibernate</groupId>
    			<artifactId>hibernate-entitymanager</artifactId>
    			<version>4.1.9.Final</version>
    		</dependency>
    	</dependencies>
    	<build>
    		<finalName>SpringJPA</finalName>
    	</build>
    </project>
  3. Create the Java classes EmployeeService.java, EmployeeServiceImpl.java, EmployeeRepository.java, Employee.java, and App.java under com.javainterviewpoint folder.

Other interesting articles which you may like …

  • Spring RESTful Web Services CRUD Example
  • Springfox Swagger 2 for Spring RESTful Web Services
  • Spring Boot CRUDRepository Example
  • Spring Data JPA Many To Many Foreign Key Example
  • Spring PropertyPlaceholderConfigurer Example
  • Spring JdbcTemplate Example + JdbcDaoSupport
  • Spring CRUD Example with JdbcTemplate + Maven + Oracle
  • @Autowired, @Resource, @Qualifier, @Inject Annotation
  • Spring Bean Life Cycle – Bean Initialization and Destruction
  • Static Factory Method & Instance Factory Method
  • How to create Spring Beans Using Spring FactoryBean
  • How to specify Spring Bean Reference and Spring Inner Bean
  • Spring Dependency Checking and Spring @Required Annotation

Spring Data JPA CRUD Example

EmployeeRepository.java

package com.javainterviewpoint;

import org.springframework.data.jpa.repository.JpaRepository;

public interface EmployeeRepository extends JpaRepository<Employee,Integer>
{

}

We have extended JpaRepository in our EmployeeRepository class, that’s all we need to do. We will be able to perform CRUD Operations using the built-in methods of JpaRepository.

EmployeeService.java

package com.javainterviewpoint;

import java.util.List;

public interface EmployeeService
{
    public abstract Employee getEmployeeById(int id);
    public abstract void saveEmployee(Employee employee);
    public abstract void updateEmployee(Employee employee);
    public abstract void deleteEmployee(int id);
    public abstract List<Employee> getAllEmployees();
}

EmployeeService is a simple interface to perform CRUD Operation, implementation will be provided by EmployeeServiceImpl class.

EmployeeServiceImpl.java

package com.javainterviewpoint;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class EmployeeServiceImpl implements EmployeeService
{
    @Autowired
    private EmployeeRepository employeeRepository;
    
    @Transactional
    public Employee getEmployeeById(int id)
    {
        return employeeRepository.findOne(id);
    }
    @Transactional
    public void saveEmployee(Employee employee)
    {
        employeeRepository.save(employee);
    }
    @Transactional
    public void updateEmployee(Employee employee)
    {
        employeeRepository.save(employee);       
    }
    @Transactional
    public void deleteEmployee(int id)
    {
        employeeRepository.delete(id);
    }
    @Transactional
    public List<Employee> getAllEmployees()
    {
       return employeeRepository.findAll();
    }
}

We have implemented the EmployeeService interface and auto-wired EmployeeRepository class. We will be using findOne(), save(), delete(), findAll() method of JpaRepository class to perform CRUD.

SpringConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc		http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
		http://www.springframework.org/schema/beans		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx/	http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/context	http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/mvc	http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/data/jpa	http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd">

     <context:component-scan base-package="com.javainterviewpoint"></context:component-scan>
     <jpa:repositories base-package="com.javainterviewpoint"
        entity-manager-factory-ref="entityManagerFactoryBean"></jpa:repositories>
     <bean id="employeeServiceImpl" class="com.javainterviewpoint.EmployeeServiceImpl" />

     <!--EntityManagerFactory -->
     <bean id="entityManagerFactoryBean"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- This makes /META-INF/persistence.xml is no longer necessary -->
        <property name="packagesToScan" value="com.javainterviewpoint" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
        <props>
           <prop key="hibernate.hbm2ddl.auto">update</prop>
           <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
        </props>
        </property>
      </bean>

     <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
         <property name="url" value="jdbc:oracle:thin:@rsh2:40051:mydb" />
         <property name="username" value="root" />
         <property name="password" value="root" />
      </bean>

      <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
         <property name="entityManagerFactory" ref="entityManagerFactoryBean" />
      </bean>
  <tx:annotation-driven />
</beans>

Below are the configurations which has been made in the above config file.

  • Configure the data source bean
  <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
         <property name="url" value="jdbc:oracle:thin:@rsh2:40051:mydb" />
         <property name="username" value="root" />
         <property name="password" value="root" />
      </bean>
  • Configure the entity manager factory bean
 <bean id="entityManagerFactoryBean"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- This makes /META-INF/persistence.xml is no longer necessary -->
        <property name="packagesToScan" value="com.javainterviewpoint" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
        <props>
           <prop key="hibernate.hbm2ddl.auto">update</prop>
           <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
        </props>
        </property>
      </bean>
  • Configure the transaction manager bean
   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
         <property name="entityManagerFactory" ref="entityManagerFactoryBean" />
      </bean>
  • In the <jpa:repositories> tag we need to pass our “entityManagerFactoryBean” reference
      <jpa:repositories base-package="com.javainterviewpoint"
        entity-manager-factory-ref="entityManagerFactoryBean"></jpa:repositories>

App.java

package com.javainterviewpoint;

import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App 
{
    public static void main( String[] args )
    {
        //Reading the Configuration file
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("springConfig.xml");
        
        //Get the EmployeeServiceImpl bean
        EmployeeServiceImpl employeeServiceImpl = (EmployeeServiceImpl)context.getBean("employeeServiceImpl");
        
        //Create Employee object
        Employee employee = new Employee();
        employee.setAge(55);
        employee.setDept("Blogging");
        employee.setName("JIP");
        
        //Save the new Employee
        employeeServiceImpl.saveEmployee(employee);
        //Read the Employee 
        Employee emp = employeeServiceImpl.getEmployeeById(1);
        //Retrieve all the Employees
        List<Employee> employeeList = employeeServiceImpl.getAllEmployees();
        System.out.println("*** List of all Employees *** ");
        for(Employee emp1 : employeeList)
        {
            System.out.println("Employee Id   :"+emp1.getId());
            System.out.println("Employee Name :"+emp1.getName());
            System.out.println("Employee Age  :"+emp1.getAge());
            System.out.println("Department :"+emp1.getDept());
            System.out.println();
        }
        System.out.println("*************************************");
        
        //Delete Employee
        employeeServiceImpl.deleteEmployee(1);
        
        context.close();
    }
}
  • ClassPathXmlApplicationContext  reads our Configuration File(SpringConfig.xml)
  • BeanFactory class read all the bean definition mentioned in the config file.
  • Get the EmployeeServiceImpl Class instance by calling the getBean() method over the bean factory.
  • We will be calling the saveEmployee(), getEmployeeById(), getAllEmployees() and deleteEmployee() of the EmployeeServiceImpl Class.

Employee.java

package com.javainterviewpoint;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="EMPLOYEE")
public class Employee implements Serializable 
{
    private static final long serialVersionUID = -889976693182180703L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="ID")
    private int id;
    
    @Column(name="NAME")
    private String name;
    
    @Column(name="AGE")
    private int age;
    
    @Column(name="DEPT")
    private String dept;
    
    public Employee()
    {
        super();
    }

    public Employee(int id, String name, int age, String dept)
    {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.dept = dept;
    }
    
    public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id = id;
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public int getAge()
    {
        return age;
    }
    public void setAge(int age)
    {
        this.age = age;
    }
    public String getDept()
    {
        return dept;
    }
    public void setDept(String dept)
    {
        this.dept = dept;
    }
    @Override
    public String toString()
    {
        return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", dept=" + dept + "]";
    }
    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((dept == null) ? 0 : dept.hashCode());
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Employee other = (Employee) obj;
        if (age != other.age)
            return false;
        if (dept == null)
        {
            if (other.dept != null)
                return false;
        } else if (!dept.equals(other.dept))
            return false;
        if (id != other.id)
            return false;
        if (name == null)
        {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
}

Our Employee class is a simple POJO class consisting getters and setters of its properties.

Output

*** List of all Employees *** 
Employee Id   :1
Employee Name :JIP1
Employee Age  :99
Department :IT

Employee Id   :4
Employee Name :aaaa
Employee Age  :4
Department :test

Employee Id   :3
Employee Name :JIP3333
Employee Age  :33333
Department :IT

Employee Id   :141
Employee Name :JIP
Employee Age  :55
Department :Blogging

*************************************

    Download Source Code

Filed Under: J2EE, Java, Spring Data, Spring Tutorial Tagged With: Spring Data, Spring Data JPA CRUD

Comments

  1. arun singh says

    February 21, 2018 at 8:48 pm

    Thanks, a big help

    Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Java Basics

  • JVM Architecture
  • Object in Java
  • Class in Java
  • How to Set Classpath for Java in Windows
  • Components of JDK
  • Decompiling a class file
  • Use of Class.forName in java
  • Use Class.forName in SQL JDBC

Oops Concepts

  • Inheritance in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java
  • Hybrid Inheritance in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of Polymorphism in java
  • Method Overriding in Java
  • Can we Overload static methods in Java
  • Can we Override static methods in Java
  • Java Constructor Overloading
  • Java Method Overloading Example
  • Encapsulation in Java with Example
  • Constructor in Java
  • Constructor in an Interface?
  • Parameterized Constructor in Java
  • Constructor Chaining with example
  • What is the use of a Private Constructors in Java
  • Interface in Java
  • What is Marker Interface
  • Abstract Class in Java

Java Keywords

  • Java this keyword
  • Java super keyword
  • Final Keyword in Java
  • static Keyword in Java
  • Static Import
  • Transient Keyword

Miscellaneous

  • newInstance() method
  • How does Hashmap works internally in Java
  • Java Ternary operator
  • How System.out.println() really work?
  • Autoboxing and Unboxing Examples
  • Serialization and Deserialization in Java with Example
  • Generate SerialVersionUID in Java
  • How to make a class Immutable in Java
  • Differences betwen HashMap and Hashtable
  • Difference between Enumeration and Iterator ?
  • Difference between fail-fast and fail-safe Iterator
  • Difference Between Interface and Abstract Class in Java
  • Difference between equals() and ==
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Sort Objects in a ArrayList using Java Comparator

Follow

  • Coding Utils

Useful Links

  • Spring 4.1.x Documentation
  • Spring 3.2.x Documentation
  • Spring 2.5.x Documentation
  • Java 6 API
  • Java 7 API
  • Java 8 API
  • Java EE 5 Tutorial
  • Java EE 6 Tutorial
  • Java EE 7 Tutorial
  • Maven Repository
  • Hibernate ORM

About JavaInterviewPoint

javainterviewpoint.com is a tech blog dedicated to all Java/J2EE developers and Web Developers. We publish useful tutorials on Java, J2EE and all latest frameworks.

All examples and tutorials posted here are very well tested in our development environment.

Connect with us on Facebook | Privacy Policy | Sitemap

Copyright ©2023 · Java Interview Point - All Rights Are Reserved ·