================= Spring Data JPA ================= JPA : Java Persistence API => JPA is used to communicate with Databases by using Java applications. => We Have several options to communicate with database using java applications 1) JDBC API (boiler plate code) 2) Spring JDBC (we have to write queries, data will be represented in text format) 3) Hibernate (ORM -> Object Relational Mapping) 4) Spring ORM (Internally uses Hibernate) == 2 lines 2 lines 2 lines 2 lines 5) Spring Data JPA (latest) => 1 line ========================== What is Spring Data JPA ? ========================== => Spring Data JPA is part of the Spring Data project. => Spring Data JPA is used to simplify Database Operations. => It reduces boilerplate code required to implement data access logic ### Note: By Writing just 1 line of code we can perform CRUD operations in database table. ### => Spring Data JPA internally uses JPA specification. Hibernate is an implementation for JPA. Note: Hibernate internally uses JDBC to execute SQL queries with database. Spring Data JPA --> JPA --> Hiberante --> JDBC --> Database ================================= Advantages with Spring Data JPA ================================= 1) Automatic Repository Implementation : If we create Repository interface then Data JPA will implement that in Runtime. 2) Query Methods : Allows you to define queries just by method names. public List findByEmpGender(String gender); 3) JPQL and Native Queries : You can use @Query annotation to write custom JPQL or native SQL queries. 4) Pagination and Sorting : Built-in support via Pageable and Sort parameters. 5) Transaction Management : Integrates smoothly with Spring's transaction management. ================================== Core Concepts of Spring Data JPA ================================== 1) Data Source 2) Entity 3) Repository Interfaces ===================== What is Datasource ? ===================== => DataSource represents set of connections with database. It will create connection pool. Note: We need to configure datasource properties in "application.properties" file. spring.datasource.url= spring.datasource.username= spring.datasource.password= ===================== What is Entity ? ===================== => Entity is a java class which is mapped with database table. We will use below annotations in entity class. @Entity : Represents java class as Entity class (mandatory). @Table : map class name with table name (optional). @Id: Represents the variable mapped with PK column (mandatory). @Column : Map entity class variable with db table column name (optional). ===================== What is Repository ? ===================== => Repository is an interface provided by Data JPA to perform CRUD operations. Note: In data jpa mainley we have 2 repository interfaces 1) CrudRepository : We can perform CRUD Operations 2) JpaRepository : CRUD Ops + Sorting + Pagination + Query By Example (QBE) Ex: public interface EmpRepository extends CrudRepository { } ====================== Environment Setup ====================== 1) Database Setup - MySQL DB Server - MySQL workbench (Client s/w) @@ MySQL DB Installation Video : https://www.youtube.com/watch?v=EsAIXPIsyQg @@ Oracle DB Installation Video : https://www.youtube.com/watch?v=-RrYpn-ACAk ======================================= Developing First Data JPA Application ======================================= @@ Project Lombok Video : https://www.youtube.com/watch?v=8tDym-FxU0A @@ Data JPA App development Video : https://www.youtube.com/watch?v=ZGKHCJsp4hg 1) Create SpringBoot application with below dependencies a) starter-data-jpa b) mysql-driver c) project-lombok 2) Configure datasource & ORM properties in application.properties file 3) Create Entity class 4) Create Our Repository interface by extending CrudRepository interface. 5) Create Service class and inject Repository and write required methods 6) Test app by calling service class methods from start class.