"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Spring Data JPA Date : 01/10/2024 (Session - 36) _____________________________________________________________________________________________________________________________ Today session : Spring Data =========================== * This Module contains four sub modules 1) Spring Data JPA 2) Spring Data Jdbc 3) Spring Data MongoDB 4) Spring Data Redis (Upcoming Session) * Spring Data provides Abstraction on Relational Databases & Non-Relational Databases 1) Relational Databases >>>>>> The data which we can have in structured format (Tables(Rows,Columns)) Example : Oracle,MySQL,Postgresql,DB2,SQLServer etc., * Relational Databases are also called "SQL Databases". 2) Non Relational Databases >>>>>> The data which doesn't have any proper Structure(Documents,Key-value,graphs,columns) Example : MongoDB,Redis,Neo4J,Apache Cassandar * Non-Relational Databases are also called "NoSQL Databases". For More Information about Spring Data Module i.e.,https://spring.io/projects/spring-data * Typically every Java Application contains Multiple logics 1) Presentation Logic/Presentation Layer 2) Business Logic/Service Layer 3) Secondary Logic/Intermediate Layer 4) Persistence Logic/Persistence Layer * By Using Spring Data Module we can build the "Persistence logic". * The logic which interacts with Database Software for Storing & Retrieving information is called "Persistence Logic". * Earlier If Java Application wanted to communicate with Database Software we used Core technology "JDBC". * We have some drawbacks with Jdbc Technology 1) Explictly programmer need to get the connection object & closing Connection Object in Java Jdbc Application. 2) All the Exceptions are in Jdbc Technology i.e.,Checked Exception using try,catch blocks or throws keyword. 3) While performing the Database operations such as insert,update,delete internally data will transferred in the form text. Ex: === Connection con = DriverManager.getConnection("JdbcURL","Databaseusername","DatabasePassword"); PreparedStatement pstmt = con.prepareStatement("insert into ashokit_customers values(?,?,?)"); pstmt.setInt(1,1020); pstmt.setString(2,"Ashok"); pstmt.setString(3,"Hyderabad"); 4) Writing the SQL Query and table need to be created manually when we working with Jdbc Technology. 5) ResultSet Object is not Serializable object by default.so to send the data over network we need convert the ResultSet object into another Collection object. 6) We need to write lot of boiler plate code (Repeation code). 7) Changing the database software of the project is very difficult process. 8) Whatever the queries we are writing in the Jdbc Application all queries are "Database Dependent Queries". * Inorder to overcome the problems of Jdbc Technology third party vendors provides some abstraction on Jdbc i.e.,ORM Frameworks * ORM Stands "Object Relational Mapping" its nothing but mapping the Java objects to Relational Database Table. Ex : Employee <--------------------------------------------> ashokit_emp(DB table) Customer <--------------------------------------------> ashokit_customer(DB Table) Student <--------------------------------------------> ashokit_students(DB Table) * In ORM Tools we can perform the database operation through Java Objects the advantage of passing the SQL Instructions through Java Object i.e.,Migration from one database to another database easily. * We have several ORM Frameworks are available in Market as below 1) JPA 2) Hibernate 3) TopLink 4) Ibatis 5) JDO * The Following things we can do with ORM Tools 1) Through Java Object will execute SQL Instructions. Employee emp = new Employee(); emp.setName("Mahesh"); emp.setLocation("Hyderabad"); //ORM Method save(emp); //insert the data into database table. 2) We can have option to write Database Specific Queries using "Native SQL Queries" Concept in ORM Tool. 3) We can have option to write Database Independent Queries using some Query Languages in ORM Tool Hibernate >>>> HQL(Hibernate Query Language) >>>> To write Database Independent Queries JPA >>>> JPQL(Java Persistence Query Language) >>>> To Write Database Independent Queries Example: ======= select customer_id,customer_name from ashokit_customers; >>>>>>>> SQL Query select customerId,customerName from Customer; >>>>>>>> HQL/JPQL Query column_names >>>>> Mapped To >>>>> Fields Of Java Class Table_name >>>>> Mapped To >>>>> Java Class /Entity Class 4) All exceptions in ORM Tools are converted from checked exceptions into unchecked exceptions. 5) Every ORM Tool will support for caching,Versioning,timestamping etc.., 6) Dynamic Schema Generation. 7) Generators are used to generating primary key column values automatically. Oracle ::::: By using sequence we can generate some unique number for primary key column while inserting the data into DB Table MYSQL ::::: AutoIncrement we don't have sequence concept. ORM VS JPA VS ORM FRAMEWORK =========================== ORM >>>>>> Building the Object based Persistence logic. JPA >>>>>> Nothing but specification having rules & guidelines in the form of interfaces for ORM. ORM Framework >>>>>>> Tool developed based on JPA Specification rules & Guidelines. Database Table (ashokit_customers) ================================== 1) Insert the data into table 2) Update the data into table 3) delete the data from table 4) select all the data from Table 5) select paritial data from Table based on conditions Persistence Logic using ORM =========================== 1) Create an Entity class which represents mapping between Java Class To Database table For Storing & Retreiving the data. 2) We need to create DAO Interface with some DAO methods for performing the Database Operations. createCustomer(Customer c) >>>>> Inserting the Data into table updateCustomer(Customer c) >>>>> Updating the data into Table deleteCustomer(int customerId)>>>>> deleting the data from table getAllCustomer() >>>>>>>>>>>>> Selecting all the Data from table getCustomersByCity()>>>>>>>>>>> Selecting partial data based on condition 3) The above Interface abstract method need to provide the implementation using ORM Framework in Implementation class. 4) Onces DAO Implementation class got completed we can inject DAO Object into Service Object and followed with Injecting Service object into Controller object. 100 Database Tables >>>>>>>> 100 Interfaces need to create >>>>>>>>>> 100 Implentation class need to be create (5 Methods) 100 * 5 = 500 Methods JDBC >>>>>>>>>>>>>>>>> ORM Framework(ORM, Spring ORM) >>>>>>>>>>>>>>>> Spring Data JPA 5) In Spring Data JPA we have following advantage 200 DatabaseTables >>>>> 200 Entity Classes >>>>> 200 DAO Interfaces >>>>> No Need for Creating implementation class * Before Arrival of Spring Data Module we do not have any module in spring framework to interact with No SQL Database Softwares and moreover we have option to interact with SQL Databases in spring framework Spring Jdbc (or) Spring ORM Spring Application <-------------------------------------------------> SQL Database Softwares(Oracle,MySQL,SQLServer etc.,) MongoDB/Cassandar API + Mongo/Cassandar Drivers Spring Application <-------------------------------------------------> NoSQL Database Softwares(MongoDB,Cassendar,Redis) * After Arrival of Spring Data Module which provides unified environment by using lots of sub modules to interact with SQL and NoSQL Database Software. Spring Data JDBC (or) Spring Data JPA Spring Application <------------------------------------------------> SQL Databases(Oracle,MySQL,PostgresSQL,SQLServer) Spring Data MongoDB Spring Application <------------------------------------------------> MongoDB Spring Data Neo4J Spring Application <------------------------------------------------> Neo4J Database Spring Data Cassandar Spring Application <------------------------------------------------> Cassendar Database Spring Data CouchBase Spring Application <------------------------------------------------> CouchBase Database * Spring Data Module Provides the Abstraction on both kinds of Databases(SQL,NOSQL). * As programmer when we working with Spring Data module we have lot of advantages 1) Increased Productivity ========================= * Here Programmer need to concentrate on creating Entity Classes for DB Tables & Creating DAO Interfaces for Entity class thats all. * NO need to concentrate on implementation classes of DAO Interface and no need defining common DAO methods Implementation. Ex: ashokit_customers(DB Table) <---------------------> Customer(Entity Class) <----------------> CustomerDAO(Interface) ashokit_users(DB Table) <---------------------> User(Entity Class) <----------------> UserDAO(Interface) ashokit_students(DB Table) <---------------------> Student(Entity Class) <----------------> StudentDAO(Interface) NOTE: ===== * Entity Class is nothing but POJO Class having Fields & Setters & Getters for Fields and added annotation @Entity annotation on top of the Java Class. * Entity class are purely used for Database operation only. @Entity public class Student{ //Fields & Setters & Getters } 2) Simplified Database Integration: ================================== * Spring Data provides unified environment to work with SQL Databases & No-SQL Databases. 3) Object-Relational Mapping (ORM) Support: =========================================== * Spring Data JPA leverages Hibernate as the default JPA provider, which offers powerful ORM capabilities. * It maps Java objects to database tables and provides transparent persistence, allowing developers to work with domain objects directly instead of dealing with SQL queries. 4) Integration with Spring Ecosystem ==================================== * We can easily integrate the Spring Data JPA with Spring ecosystem. * Spring Data JPA is larger submodule similar to Spring Boot,spring Security,spring MVC etc., 5) Transaction Management: ========================== * Spring Data JPA integrates seamlessly with Spring's transaction management capabilities. * This simplifies the handling of database transactions, ensuring data consistency and enabling automatic rollback in case of failures.