Spring Data JPA
Shivangam Soni
Full Stack Developer | Researching NLP | JavaScript, TypeScript, React, Next, Express, Mongo, Python
JPA
Java Persistence API (JPA) is a Java Specification for Object Relational Mapping (ORM). It provides a high-level abstraction layer for database access. JPA allows us to use POJOs (Plain Old Java Objects) & provides a standard set of APIs for performing common Database Operations with ease.
Features of JPA
How to use JPA
As mentioned above, JPA is a Java Specification hence, we need to use an Implementation of JPA like: Hibernate, EclipseLink, Spring Data JPA, etc.
All JPA Implementations require us to define Entity classes that represent Database tables. We use JPA Annotations to specify the mapping details for each Entity & the corresponding Database Table.
JPA Automatically generates the APIs for performing operations on the Database. We can use EntityManager, a core interface in JPA, to manage Entity Instances, perform operations & execute queries.
Spring Data JPA
Spring Data JPA is a powerful Library that provides a high-level abstraction on top of JPA. It combines JPA's ORM with the ease & flexibility of Spring Framework.
Spring Data JPA allows us to use expressive Methods that simplify the task of performing common database operations. It reduces the Code complexity by reducing Boilerplate Code.
Features of Spring Data JPA
Using Spring Data JPA
We are going to use Spring Data JPA to get access to Products.
package com.shivangam.ProductManagement
import jakarta.persistence.*;
@Entity
@Table(name = "product")
public class Product {
? ? @Id
? ? @GeneratedValue(strategy = GenerationType.IDENTITY)
? ? private int id;
? ? private String name;
? ? private String type;
? ? public Product() {
? ? }
? ? public Product(String name, String type, String place, int warranty) {
? ? ? ? this.name = name;
? ? ? ? this.type = type;
? ? ? ? this.place = place;
? ? ? ? this.warranty = warranty;
? ? }
? ? 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 String getType() {
? ? ? ? return type;
? ? }
? ? public void setType(String type) {
? ? ? ? this.type = type;
? ? }
}
package com.shivangam.ProductManagement
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ProductDB extends JpaRepository<Product, Integer> {
? ? Product findByNameIgnoreCase(String name);
? ? @Query("SELECT p FROM Product p WHERE LOWER(p.name) LIKE LOWER(concat('%', :text, '%')) OR LOWER(p.type) LIKE LOWER(concat('%', :text, '%'))")
? ? List<Product> findAllByText(@Param("text") String text);
}
Day 3 Challenge
Challenge
Continue development of Product Management App by implementing it using Maven, Spring Boot, Spring Data JPA & PostgreSQL.
Solution
GitHub Repo README includes the Explanation of My Solutions.
Thank You, Navin Reddy , for the 10-Day Challenge.