Spring Data JPA CRUD Example

Introduction

In this tutorial I will show you an example on Spring Data JPA CRUD. Here I will use the embedded or in-memory HSQL database. CRUD means Create, Read, Update, Delete. So I am going to perform basic CRUD operations on some entities.

In this example I will create annotation based Spring standalone application.

Related Posts:

The central interface in Spring Data repository abstraction is Repository. It takes the the domain class to manage as well as the id type of the domain class as type arguments. This interface acts primarily as a marker interface to capture the types to work with and to help you to discover interfaces that extend this one. The CrudRepository provides sophisticated CRUD functionality for the entity class that is being managed.

Prerequisites

Java at least 8, Spring 5.2.8, Spring Data JPA 2.3.3, HSQL DB 2.5.1, Gradle 6.5.1, Maven 3.6.3

Project Setup

You can create either gradle or maven based project in your favorite IDE or tool. The name of the project is spring-data-jpa-crud.

If you are creating gradle based project then you can use below build.gradle script:

plugins {
    id 'java-library'
}

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
	implementation 'org.springframework:spring-core:5.2.8.RELEASE'
	implementation 'org.springframework:spring-context:5.2.8.RELEASE'
    implementation 'org.springframework.data:spring-data-jpa:2.3.3.RELEASE'
    implementation 'org.hsqldb:hsqldb:2.5.1'
    implementation 'org.hibernate.orm:hibernate-core:6.0.0.Alpha5'
}

If you are creating maven based project then you can use below pom.xml file:

<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	
	<groupId>com.roytuts</groupId>
	<artifactId>spring-data-jpa-crud</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>at least 1.8</java.version>
	</properties>
	
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-core</artifactId>
			<version>5.2.8.RELEASE</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.2.8.RELEASE</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-jpa</artifactId>
			<version>2.3.3.RELEASE</version>
		</dependency>
		
		<dependency>
			<groupId>org.hsqldb</groupId>
			<artifactId>hsqldb</artifactId>
			<version>2.5.1</version>
		</dependency>
		
		<dependency>
			<groupId>org.hibernate.orm</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>6.0.0.Alpha5</version>
		</dependency>
	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
                <configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Database Scripts

I am using in-memory database. So I will manage table creation and data insertion through SQL scripts.

Create below schema.sql file and put it under src/main/resources directory.

DROP TABLE ProductType IF EXISTS;
CREATE TABLE ProductType (
    ProductTypeId integer identity primary key,
    ProductTypeName varchar(50) not null,
    ProductTypeDesc varchar(255) not null
);
DROP TABLE Product IF EXISTS;
CREATE TABLE Product (
    ProductId integer identity primary key,
    ProductTypeId integer,
    ProductName varchar(50) not null,
    ProductDesc varchar(255) not null,
    ProductPrice float not null
);
ALTER TABLE Product
ADD FOREIGN KEY (ProductTypeId)
REFERENCES ProductType(ProductTypeId);

Now create below data script data.sql to populate above tables and put it under src/main/resources.

INSERT INTO ProductType(ProductTypeName, ProductTypeDesc) VALUES('Mobile','All kind of Mobile Products')
INSERT INTO ProductType(ProductTypeName, ProductTypeDesc) VALUES('Tab','All kind of Tab Products')
INSERT INTO ProductType(ProductTypeName, ProductTypeDesc) VALUES('Laptop','All type of Laptops')
INSERT INTO ProductType(ProductTypeName, ProductTypeDesc) VALUES('Desktop','All type of Desktops')
INSERT INTO Product(ProductTypeId, ProductName, ProductDesc, ProductPrice) VALUES((SELECT ProductTypeId FROM ProductType WHERE ProductTypeName = 'Mobile'), 'Samsung S7', 'Samsung S7 mobile', 30000.00)
INSERT INTO Product(ProductTypeId, ProductName, ProductDesc, ProductPrice) VALUES((SELECT ProductTypeId FROM ProductType WHERE ProductTypeName = 'Tab'), 'Galaxy Tab A', 'Samsung Galaxy Tab', 32000.00)
INSERT INTO Product(ProductTypeId, ProductName, ProductDesc, ProductPrice) VALUES((SELECT ProductTypeId FROM ProductType WHERE ProductTypeName = 'Laptop'), 'Lenovo Yoga', 'Lenovo Laptop', 35340.00)
INSERT INTO Product(ProductTypeId, ProductName, ProductDesc, ProductPrice) VALUES((SELECT ProductTypeId FROM ProductType WHERE ProductTypeName = 'Desktop'), 'Dell Optiplex', 'Dell Desktop', 24800.00)
INSERT INTO Product(ProductTypeId, ProductName, ProductDesc, ProductPrice) VALUES((SELECT ProductTypeId FROM ProductType WHERE ProductTypeName = 'Desktop'), 'Samsung 18.5 Slim', 'Samsung Desktop', 5010.00)

App Configuration

In this class I am going to configure database as well as application.

package com.roytuts.spring.data.jpa.crud.config;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = "com.roytuts.spring.data.jpa.crud")
@EnableJpaRepositories(basePackages = "com.roytuts.spring.data.jpa.crud.repository")
public class Config {

	@Bean
	public DataSource dataSource() {
		EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
		EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL).addScript("classpath:schema.sql")
				.addScript("classpath:data.sql").build();
		return db;
	}

	@Bean
	public EntityManagerFactory entityManagerFactory() {
		HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
		vendorAdapter.setDatabase(Database.HSQL);
		vendorAdapter.setGenerateDdl(true);
		LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
		factory.setJpaVendorAdapter(vendorAdapter);
		factory.setPackagesToScan("com.roytuts.spring.data.jpa.crud.entity");
		factory.setDataSource(dataSource());
		factory.afterPropertiesSet();
		return factory.getObject();
	}

	@Bean
	public PlatformTransactionManager transactionManager() {
		JpaTransactionManager txManager = new JpaTransactionManager();
		txManager.setEntityManagerFactory(entityManagerFactory());
		return txManager;
	}

}

Entity Classes

Now to map database tables with Java, you need entity classes as shown below.

package com.roytuts.spring.data.jpa.crud.entity;

import java.io.Serializable;
import java.util.List;

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

@Entity
@Table(name = "ProductType")
public class ProductType implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@Column(name = "ProductTypeId")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer productTypeId;

	@Column(name = "ProductTypeName")
	private String productTypeName;

	@Column(name = "ProductTypeDesc")
	private String productTypeDesc;

	@OneToMany(cascade = CascadeType.ALL, mappedBy = "productType")
	private List<Product> products;

	//getters and setters

	@Override
	public String toString() {
		return "ProductType [productTypeId=" + productTypeId + ", productTypeName=" + productTypeName
				+ ", productTypeDesc=" + productTypeDesc + "]";
	}
}
package com.roytuts.spring.data.jpa.crud.entity;

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.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "Product")
public class Product implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@Column(name = "ProductId")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer productId;

	@Column(name = "ProductName")
	private String productName;

	@Column(name = "ProductDesc")
	private String productDesc;

	@Column(name = "ProductPrice")
	private Double productPrice;

	@ManyToOne
	@JoinColumn(name = "productType", referencedColumnName = "ProductTypeId")
	private ProductType productType;

	//getters and setters

	@Override
	public String toString() {
		return "Product [productId=" + productId + ", productName=" + productName + ", productDesc=" + productDesc
				+ ", productPrice=" + productPrice + "]";
	}

}

Spring Data JPA Repository

Spring Data JPA repository provides built-in methods for performing basic CRUD operations but if you need to execute complex/custom query then you can use @Query annotation to provide your query.

package com.roytuts.spring.data.jpa.crud.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import com.roytuts.spring.data.jpa.crud.entity.Product;

public interface ProductRepository extends JpaRepository<Product, Integer> {

	@Query("select p from Product p where p.productName = ?1")
	Product findByProductName(String productName);

	@Modifying
	@Transactional
	@Query("UPDATE Product p SET p.productName = ?1, p.productDesc = ?2, p.productPrice = ?3"
			+ " WHERE p.productId = ?4")
	void updateProduct(String name, String desc, Double price, Integer id);

	@Modifying
	@Transactional
	@Query("DELETE FROM Product p WHERE p.productId = :id")
	void deleteProduct(@Param("id") Integer id);

}
package com.roytuts.spring.data.jpa.crud.repository;

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

import com.roytuts.spring.data.jpa.crud.entity.ProductType;

public interface ProductTypeRepository extends JpaRepository<ProductType, Integer> {

	@Query("select pt from ProductType pt where pt.productTypeName = ?1")
	ProductType findByProductTypeName(String productTypeName);

	// if you want case insensitive
	@Query("select pt from ProductType pt where upper(pt.productTypeName) = upper(?1)")
	ProductType findByProductTypeNameCaseInsensitive(String productTypeName);

}

Spring Service Class

Service class is required to process business requirements for the application.

package com.roytuts.spring.data.jpa.crud.service;

import java.util.List;

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

import com.roytuts.spring.data.jpa.crud.entity.Product;
import com.roytuts.spring.data.jpa.crud.repository.ProductRepository;

@Service
public class ProductService {

	@Autowired
	private ProductRepository productRepository;

	public List<Product> getAllProducts() {
		List<Product> products = productRepository.findAll();
		return products;
	}

	public Product findProductById(int id) {
		return productRepository.findById(id).get();
	}

	public Product findProductByName(String productName) {
		return productRepository.findByProductName(productName);
	}

	@Transactional
	public void saveProduct(Product product) {
		productRepository.save(product);
	}

	@Transactional
	public void updateProduct(Product product) {
		productRepository.updateProduct(product.getProductName(), product.getProductDesc(), product.getProductPrice(),
				product.getProductId());
	}

	@Transactional
	public void deleteProduct(Product product) {
		productRepository.deleteProduct(product.getProductId());
	}

}
package com.roytuts.spring.data.jpa.crud.service;

import java.util.List;

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

import com.roytuts.spring.data.jpa.crud.entity.ProductType;
import com.roytuts.spring.data.jpa.crud.repository.ProductTypeRepository;

@Service
public class ProductTypeService {

	@Autowired
	private ProductTypeRepository productTypeRepository;

	public List<ProductType> getAllProductTypes() {
		return productTypeRepository.findAll();
	}

	public ProductType findProductTypeById(int id) {
		return productTypeRepository.findById(id).get();
	}

	public ProductType findProductTypeByName(String productTypeName) {
		return productTypeRepository.findByProductTypeName(productTypeName);
	}

	public ProductType findProductTypeByNameIgnoreCase(String productTypeName) {
		return productTypeRepository.findByProductTypeNameCaseInsensitive(productTypeName);
	}

	@Transactional
	public void saveProductType(ProductType productType) {
		productTypeRepository.save(productType);
	}

	@Transactional
	public void updateProductType(ProductType productType) {
		productTypeRepository.save(productType);
	}

	@Transactional
	public void deleteProductType(ProductType productType) {
		productTypeRepository.delete(productType);
	}

}

Main Class

I am creating below main class to test the Spring Data JPA CRUD application.

package com.roytuts.spring.data.jpa.crud;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;

import com.roytuts.spring.data.jpa.crud.config.Config;
import com.roytuts.spring.data.jpa.crud.entity.Product;
import com.roytuts.spring.data.jpa.crud.entity.ProductType;
import com.roytuts.spring.data.jpa.crud.service.ProductService;
import com.roytuts.spring.data.jpa.crud.service.ProductTypeService;

@Configuration
public class SpringDataJpaCrudApp {

	public static void main(String[] args) {
		ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
		ProductService productService = context.getBean(ProductService.class);
		ProductTypeService productTypeService = context.getBean(ProductTypeService.class);

		// ProductTypes
		List<ProductType> productTypes = productTypeService.getAllProductTypes();
		System.out.println("All Product Types");
		System.out.println("-------------------------------------------------------------");
		productTypes.forEach(pt -> System.out.println(pt));
		System.out.println("-------------------------------------------------------------");

		ProductType productType = productTypeService.findProductTypeById(1);
		System.out.println("Product Type for type id 1");
		System.out.println("-------------------------------------------------------------");
		System.out.println(productType);
		System.out.println("-------------------------------------------------------------");

		ProductType productType2 = productTypeService.findProductTypeByName("Tab");
		System.out.println("Product Type for type name Tab");
		System.out.println("-------------------------------------------------------------");
		System.out.println(productType2);
		System.out.println("-------------------------------------------------------------");

		ProductType productType3 = productTypeService.findProductTypeByNameIgnoreCase("tab");
		System.out.println("Product Types for type name tab");
		System.out.println("-------------------------------------------------------------");
		System.out.println(productType3);
		System.out.println("-------------------------------------------------------------");
		
		ProductType productType4 = new ProductType();
		productType4.setProductTypeName("Software");
		productType4.setProductTypeDesc("All types of sotwares");
		productTypeService.saveProductType(productType4);
		System.out.println("-------------------------------------------------------------");
		System.out.println("Product Type successfully saved");
		System.out.println("-------------------------------------------------------------");

		List<ProductType> productTypes2 = productTypeService.getAllProductTypes();
		System.out.println("All Product Types");
		System.out.println("-------------------------------------------------------------");
		productTypes2.forEach(pt -> System.out.println(pt));
		System.out.println("-------------------------------------------------------------");

		ProductType productType5 = new ProductType();
		productType5.setProductTypeId(4);
		productType5.setProductTypeName("Software");
		productType5.setProductTypeDesc("All types of sotware products");
		productTypeService.updateProductType(productType5);
		System.out.println("-------------------------------------------------------------");
		System.out.println("Product Type successfully updated");
		System.out.println("-------------------------------------------------------------");

		List<ProductType> productTypes3 = productTypeService.getAllProductTypes();
		System.out.println("All Product Types");
		System.out.println("-------------------------------------------------------------");
		productTypes3.forEach(pt -> System.out.println(pt));
		System.out.println("-------------------------------------------------------------");

		ProductType productType6 = new ProductType();
		productType6.setProductTypeId(4);
		productTypeService.deleteProductType(productType6);
		System.out.println("-------------------------------------------------------------");
		System.out.println("Product Type successfully deleted");
		System.out.println("-------------------------------------------------------------");

		List<ProductType> productTypes4 = productTypeService.getAllProductTypes();
		System.out.println("All Product Types");
		System.out.println("-------------------------------------------------------------");
		productTypes4.forEach(pt -> System.out.println(pt));
		System.out.println("-------------------------------------------------------------");

		// Products
		List<Product> products = productService.getAllProducts();
		System.out.println("All Products");
		System.out.println("-------------------------------------------------------------");
		products.forEach(p -> System.out.println(p));
		System.out.println("-------------------------------------------------------------");

		Product product = productService.findProductById(1);
		System.out.println("Product for id 1");
		System.out.println("-------------------------------------------------------------");
		System.out.println(product);
		System.out.println("-------------------------------------------------------------");

		product = productService.findProductByName("Samsung S7");
		System.out.println("Product for name Samsung S7");
		System.out.println("-------------------------------------------------------------");
		System.out.println(product);
		System.out.println("-------------------------------------------------------------");

		productType = productTypeService.findProductTypeById(0);
		product = new Product();
		product.setProductName("Gionee P5");
		product.setProductPrice(7750d);
		product.setProductDesc("Gionee 4G Volte Mobile");
		product.setProductType(productType);
		productService.saveProduct(product);
		System.out.println("-------------------------------------------------------------");
		System.out.println("Product successfully saved");
		System.out.println("-------------------------------------------------------------");

		products = productService.getAllProducts();
		System.out.println("All Products");
		System.out.println("-------------------------------------------------------------");
		products.forEach(p -> System.out.println(p));
		System.out.println("-------------------------------------------------------------");

		product = productService.findProductById(5);
		product.setProductName("Gionee P5");
		product.setProductPrice(7800d);
		product.setProductDesc("Gionee 4G Volte Mobile");
		productService.updateProduct(product);
		System.out.println("-------------------------------------------------------------");
		System.out.println("Product successfully updated");
		System.out.println("-------------------------------------------------------------");

		products = productService.getAllProducts();
		System.out.println("All Products");
		System.out.println("-------------------------------------------------------------");
		products.forEach(p -> System.out.println(p));
		System.out.println("-------------------------------------------------------------");

		product = productService.findProductById(5);
		productService.deleteProduct(product);
		System.out.println("-------------------------------------------------------------");
		System.out.println("Product successfully deleted");
		System.out.println("-------------------------------------------------------------");

		products = productService.getAllProducts();
		System.out.println("All Products");
		System.out.println("-------------------------------------------------------------");
		products.forEach(p -> System.out.println(p));
		System.out.println("-------------------------------------------------------------");

		((ConfigurableApplicationContext) context).close();
	}

}

Testing the Application

Run the above main class and you will see the output shown as below:

All Product Types
-------------------------------------------------------------
ProductType [productTypeId=0, productTypeName=Mobile, productTypeDesc=All kind of Mobile Products]
ProductType [productTypeId=1, productTypeName=Tab, productTypeDesc=All kind of Tab Products]
ProductType [productTypeId=2, productTypeName=Laptop, productTypeDesc=All type of Laptops]
ProductType [productTypeId=3, productTypeName=Desktop, productTypeDesc=All type of Desktops]
-------------------------------------------------------------
Product Type for type id 1
-------------------------------------------------------------
ProductType [productTypeId=1, productTypeName=Tab, productTypeDesc=All kind of Tab Products]
-------------------------------------------------------------
Product Type for type name Tab
-------------------------------------------------------------
ProductType [productTypeId=1, productTypeName=Tab, productTypeDesc=All kind of Tab Products]
-------------------------------------------------------------
Product Types for type name tab
-------------------------------------------------------------
ProductType [productTypeId=1, productTypeName=Tab, productTypeDesc=All kind of Tab Products]
-------------------------------------------------------------
-------------------------------------------------------------
Product Type successfully saved
-------------------------------------------------------------
All Product Types
-------------------------------------------------------------
ProductType [productTypeId=0, productTypeName=Mobile, productTypeDesc=All kind of Mobile Products]
ProductType [productTypeId=1, productTypeName=Tab, productTypeDesc=All kind of Tab Products]
ProductType [productTypeId=2, productTypeName=Laptop, productTypeDesc=All type of Laptops]
ProductType [productTypeId=3, productTypeName=Desktop, productTypeDesc=All type of Desktops]
ProductType [productTypeId=4, productTypeName=Software, productTypeDesc=All types of sotwares]
-------------------------------------------------------------
-------------------------------------------------------------
Product Type successfully updated
-------------------------------------------------------------
All Product Types
-------------------------------------------------------------
ProductType [productTypeId=0, productTypeName=Mobile, productTypeDesc=All kind of Mobile Products]
ProductType [productTypeId=1, productTypeName=Tab, productTypeDesc=All kind of Tab Products]
ProductType [productTypeId=2, productTypeName=Laptop, productTypeDesc=All type of Laptops]
ProductType [productTypeId=3, productTypeName=Desktop, productTypeDesc=All type of Desktops]
ProductType [productTypeId=4, productTypeName=Software, productTypeDesc=All types of sotware products]
-------------------------------------------------------------
-------------------------------------------------------------
Product Type successfully deleted
-------------------------------------------------------------
All Product Types
-------------------------------------------------------------
ProductType [productTypeId=0, productTypeName=Mobile, productTypeDesc=All kind of Mobile Products]
ProductType [productTypeId=1, productTypeName=Tab, productTypeDesc=All kind of Tab Products]
ProductType [productTypeId=2, productTypeName=Laptop, productTypeDesc=All type of Laptops]
ProductType [productTypeId=3, productTypeName=Desktop, productTypeDesc=All type of Desktops]
-------------------------------------------------------------
All Products
-------------------------------------------------------------
Product [productId=0, productName=Samsung S7, productDesc=Samsung S7 mobile, productPrice=30000.0]
Product [productId=1, productName=Galaxy Tab A, productDesc=Samsung Galaxy Tab, productPrice=32000.0]
Product [productId=2, productName=Lenovo Yoga, productDesc=Lenovo Laptop, productPrice=35340.0]
Product [productId=3, productName=Dell Optiplex, productDesc=Dell Desktop, productPrice=24800.0]
Product [productId=4, productName=Samsung 18.5 Slim, productDesc=Samsung Desktop, productPrice=5010.0]
-------------------------------------------------------------
Product for id 1
-------------------------------------------------------------
Product [productId=1, productName=Galaxy Tab A, productDesc=Samsung Galaxy Tab, productPrice=32000.0]
-------------------------------------------------------------
Product for name Samsung S7
-------------------------------------------------------------
Product [productId=0, productName=Samsung S7, productDesc=Samsung S7 mobile, productPrice=30000.0]
-------------------------------------------------------------
-------------------------------------------------------------
Product successfully saved
-------------------------------------------------------------
All Products
-------------------------------------------------------------
Product [productId=0, productName=Samsung S7, productDesc=Samsung S7 mobile, productPrice=30000.0]
Product [productId=1, productName=Galaxy Tab A, productDesc=Samsung Galaxy Tab, productPrice=32000.0]
Product [productId=2, productName=Lenovo Yoga, productDesc=Lenovo Laptop, productPrice=35340.0]
Product [productId=3, productName=Dell Optiplex, productDesc=Dell Desktop, productPrice=24800.0]
Product [productId=4, productName=Samsung 18.5 Slim, productDesc=Samsung Desktop, productPrice=5010.0]
Product [productId=5, productName=Gionee P5, productDesc=Gionee 4G Volte Mobile, productPrice=7750.0]
-------------------------------------------------------------
-------------------------------------------------------------
Product successfully updated
-------------------------------------------------------------
All Products
-------------------------------------------------------------
Product [productId=0, productName=Samsung S7, productDesc=Samsung S7 mobile, productPrice=30000.0]
Product [productId=1, productName=Galaxy Tab A, productDesc=Samsung Galaxy Tab, productPrice=32000.0]
Product [productId=2, productName=Lenovo Yoga, productDesc=Lenovo Laptop, productPrice=35340.0]
Product [productId=3, productName=Dell Optiplex, productDesc=Dell Desktop, productPrice=24800.0]
Product [productId=4, productName=Samsung 18.5 Slim, productDesc=Samsung Desktop, productPrice=5010.0]
Product [productId=5, productName=Gionee P5, productDesc=Gionee 4G Volte Mobile, productPrice=7800.0]
-------------------------------------------------------------
-------------------------------------------------------------
Product successfully deleted
-------------------------------------------------------------
All Products
-------------------------------------------------------------
Product [productId=0, productName=Samsung S7, productDesc=Samsung S7 mobile, productPrice=30000.0]
Product [productId=1, productName=Galaxy Tab A, productDesc=Samsung Galaxy Tab, productPrice=32000.0]
Product [productId=2, productName=Lenovo Yoga, productDesc=Lenovo Laptop, productPrice=35340.0]
Product [productId=3, productName=Dell Optiplex, productDesc=Dell Desktop, productPrice=24800.0]
Product [productId=4, productName=Samsung 18.5 Slim, productDesc=Samsung Desktop, productPrice=5010.0]
-------------------------------------------------------------

Source Code

Download

Thanks for reading.

1 thought on “Spring Data JPA CRUD Example

Leave a Reply

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