It is used to develop persistence logic in our application.
-> The logic which is responsible to communicate with database is called as Persistence Logic.
-> Already we have JDBC, Spring JDBC, Hibernate, Spring ORM to develop persistence logic.
-> If we use JDBC or Spring JDBC or Hibernate or Spring ORM then we should common logics in all DAO classes to perform CURD Operations.
-> Data JPA simplified persistence logic development by providing pre-defined interfaces with methods.
-> Data JPA provided Repository methods to perform CURD operations.
Note: If we use Data JPA then we don't need to write logic to perform curd operations bcz Data JPA will take care of that.
-> Data JPA provided Repository interfaces to perform CURD operations
1) CrudRepository (Methods to perform Crud Operations)
2) JpaRepository (Methods to perform Crud Operations + Pagination + Sorting + QBE)
Hibernate Vs Data JPA
++++++++++++++++++
-> In Hibernate we should implement all methods to perform CRUD operations.
-> Data JPA providing predefined methods to perform CRUD Operations.
-> In Hibernate we should boiler plate code (same code in multiple classes).
-> In Data JPA we don't need to write any method because JPA Repositories providing methods for us.
Environment Setup
+++++++++++++++++
1) MySQL Database (Server s/w)
2) MySQL Workbench (Client s/w)
MySQL DB Properties
++++++++++++++++++
Host : localhost
Port : 3306
Username : admin
Password : Tiger@123
Entity Class
++++++++++
-> The java class which is mapped with DB table is called as Entity class.
-> To map java class with DB table we will use below annotations.
@Entity : It represents our class as Entity class. (It is mandatory annotation)
@Table : It is used to map our class with DB Table name
Note : @Table is optional if our class name and table is same. If we don't give @Table then it will consider class name as table name.
@Id : It represents variable mapping with primary column in table (It is mandatory annotation).
@Column : It is used to map our class variables with DB table column names
Note: @Column is optional if class variable name and DB table column names are same. If we don't give @Column then it will consider variable name as column name.
Note: For every table we should create one Entity class. Entity class represents DB operations should be performed in which table.
@Entity
@Table(name="CRICKET_PLAYERS")
public class Player {
@Id
@Column(name="PLAYER_ID")
private Integer playerId;
@Column(name="PLAYER_NAME")
private String playerName;
@Column(name="PLAYER_AGE")
private Integer playerAge;
@Column(name="LOCATION")
private String location;
}
Note: One entity class will be mapped with only one DB table.
Repository Interfaces
+++++++++++++++++
-> JPA provided repository interfaces to perform Curd Operations.
-> For Every DB table we will create one Repository interface by extending JPA Repository
Syntax:
---------
public interface PlayerRepository extends CrudRepository<Entity, ID>{ }
Example
-----------
public interface PlayerRepository extends CrudRepository<Player, Serializable>{ }
Note: When our interface extending properties from JPA Repository interfaces then JPA will provide implementation for our interface in Runtime using Proxy Design Pattern.
Datasource properties
++++++++++++++++++++
-> Datasource properties represents with which database we want connect
- DB URL
- DB Uname
- DB Pwd
- DB Driver Class
-> We will configure datasource properties in application.properties file or application.yml file.
ORM Properties
++++++++++++++
-> Hibernate provided some additional benefits while developing persistence logic.
-> Tables can be created dynamically using "auto-ddl" property.
-> We are calling JPA methods to perform DB operations. Those methods will generate queries to execute. To print those queries on console we can use 'show-sql' property.
Jpa First Example with Oracle : https://youtu.be/99FG2QEmbJQ
Build First App using Data JPA
++++++++++++++++++++++++++
1) Create spring boot application with below dependencies
a) starter-data-jpa
b) mysql-connector
2) Create Entity class using Annotations.
3) Create Repository interface by extending CrudRepository.
4) Configure DataSource & ORM properties in application.properties file.
5) Call Repository methods in start class to perform DB operations.
Comments
Post a Comment