Part 2 - Spring Data JPA
Part 2 - Spring Data JPA
com/c/javaexpress
+91 7801007910
Java Express
Create Java classes annotated with JPA annotations to represent your database
entities.
Define repository interfaces extending Spring Data JPA interfaces
like JpaRepository or CrudRepository.
Spring Data JPA automatically generates implementations for these interfaces at
runtime, handling data access logic based on the methods you define.
www.youtube.com/c/javaexpress
+91 7801007910
Java Express
Database Connection Properties:
spring.datasource.url=jdbc:mysql://localhost:3306/devflipkart?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=India@123
spring.jpa.hibernate.ddl-auto=update
JPA Annotations:
1. @Entity:
The java class which represents DB Table structure is called as Entity class
The class which is mapped with DB Table is called as Entity Class
It is class level annotation
It is mandatory annotation
2. @Table:
It is used to map class name to particular name
It is class level annotation
It is optional annotation
If @Table is not mentioned then SB Data considers our class name as table
name
3. @Id:
It is represent variable which mapped with PK
It is field level annotation
It is mandatory annotation
4. @Column:
It is used to map entity class variable name with table column name
It is field level annotation
It is optional annotation
If we don't use @Column, SB data will consider variable name as column
name
5. @GeneratedValue (strategy = GenerationType.IDENTITY)
It is database dependent
Doesn't support for Oracle
Supports for MySQL
Schema Generation
Spring data JPA not only provides implementation for commonly used methods but
also provides a way to add custom methods.
Spring Data JPA QueryDSL:
www.youtube.com/c/javaexpress
+91 7801007910
Java Express
Development Steps
1. Create Spring Boot Project
a. groupid : com.javaexpress
b. artifactid : any name
c. version : Java 17
d. Type : maven
e. Packaging : jar
f. Language: java
2. Add two dependencies
a. Spring-boot-starter-data-jpa
b. MySQL-connector-j driver
3. Add Database properties in application.properties file
4. Create model package
a. Add Model Class
5. Create repository package
www.youtube.com/c/javaexpress
+91 7801007910
Java Express
a. Add repository interface
6. Do operations in main methods
Maven Dependencies:
Database Properties
Project Structure
www.youtube.com/c/javaexpress
+91 7801007910
Java Express
Model Class
Repository Interface
www.youtube.com/c/javaexpress
+91 7801007910
Java Express
Main Method Logic