MENU

Fun & Interesting

Spring Data JPA tutorial - Unboxing Spring Data JPA Magic | Spring Boot | JPA | MySQL | Hibernate

Selenium Express 37,290 2 years ago
Video Not Working? Fix It Now

In this video, we will unbox the magic of spring data jpa. The Goal here is to understand how Spring Data JPA works internally. This spring data jpa tutorial will give a step-by-step walkthrough on 1. Build a spring project 2. Configuring Hibernate / JPA / Spring / MYSQL with our Java spring project. 3. Doing CRUD operation with Spring Data JPA 4. Once done, Converting the spring project to Spring Boot 5. Explore spring data jpa with spring boot 6. How spring data JPA works internally If you want to build a spring boot project that connects to a database, the first thing you will create is a data source. Datasource in Java holds the connection objects. We can use a DriverManagerDatasource in spring to create a datasource object. Once the Datasouce setup is done, we can create a EntityManager, which will help us with CRUD operation. EntityManager manages our entities (alternatively called persistence context) and provides a wide range of methods, like persist, remove, find, merge,findAll, etc. This will help us to save, delete, update, and query data from a database. To create an EnityManager, we can create an EntityManagerFactory first. We only can create an EntityManager from an EntityManagerFactory. In Spring, we need a LocalcontainerEntityManagerFactoryBean to initalize an entity manager factory. Once the LocalcontainerEntityManagerFactoryBean is set up, then it's time to set up a TransactionManager (PlatformTransactionManager). We can use the JpaTransactionManager to feed it the entity manager factory object to have a fully functional connection setup. In our DAO, We can use now use an EntityManager with a @PersistenceContext annotation. This will enable us to call the API methods to do CRUD operation. We can use a @Transactional Annotation on top of our API method to activate and begin/commit the transaction. Spring Data JPA enables us to automatic repository creation. With the @EnableJpaRepositories we no longer need to write repository classes and the basic CRUD methods to perform data manipulation. Spring Data JPA will do it all and allows us to have a fully functional CRUD Repository just by extending JpaRepository. When our custom repository interface extends JpaRepository, it inherits different methods from its super interfaces (CrudRepository, ListCrudRepository, Repository). These methods' implementation is generated in runtime by Spring. Spring Generates this implementation class by using JDK dynamic proxy / AOP proxy. Data Jpa configuration is so easy when it comes to Spring Boot. We have a one-stop solution to add all the dependencies. We can use a spring boot starter project called "spring boot starter data jpa," and it brings in all the different dependencies that we need to develop a JPA application with spring boot. For MySQL db, we can just add the MySQL driver, and we are ready to go. We may need to use @EntityScan and @EnableJpaRepositories to scan our Entities and Repository classes. Let's explore all these concepts in this video! Let's jump right in. Timestamp -------------- Introduction - 00:00:00 Build a Maven project - 00:05:34 Building an Entity - 00:07:00 JPA setup - jakarta.persistence-api - 00:08:48 Setting up Repository and EntityManager - 00:13:50 LocalContainerEntityManagerFactoryBean and DataSource setup- 00:23:36 Introducing Hibernate - 00:47:56 Uses of @PersistenceContext - 00:56:42 @Transactional / TransactionManager setup - 01:02:27 Introducing JpaTransactionManager - 01:06:00 EntityManager persist method in action (save) - 01:13:00 Introducing Spring Data JPA - 01:20:00 findAll() implementation with JPA - 01:24:26 Why Spring Data JPA: The Need - 01:29:51 @EnableJpaRepository - 01:37:01 Doing CRUD with Spring Data JPA - 01:38:20 spring data jpa with spring boot - 01:46:19 Spring Boot data jpa autoconfiguration - 01:53:07 @EntityScan - 01:59:04 What's inside the spring-boot-starter-data-jpa dependency? Activating show-sql in Spring / Spring boot app -02:05:34 The End - 02:07:02 #springdatajpa #springboot #jpa #hibernate For More free/paid courses visit www.seleniumexpress.com LINKS AND FREE LEARNING RESOURCES ======== Watch my new mock interview series for java developers https://youtu.be/yX2w-Sof95s Watch Spring boot Hot topics https://youtu.be/CUQWZHjuq-w Java Interview / Question and Answer series https://youtu.be/Y1uGGTrBYKw spring core https://www.youtube.com/watch?v=r2Q0Jzl2qMQ&list=PL3NrzZBjk6m-nYX072dSaGfyCJ59Q5TEi spring mvc https://www.youtube.com/watch?v=zCHrZdJODg4&list=PL3NrzZBjk6m-OSeL3DRMD_ISa0u-9c1l1 spring mvc intermediate https://www.youtube.com/watch?v=40YaLHbrwrY&list=PL3NrzZBjk6m_2jw8sC0BkEwSPFHxgQDWr For more courses visit https://www.seleniumexpress.com you can ask for support in my website forum https://www.seleniumexpress.com Subscribe to my youtube channel https://www.youtube.com/channel/UCpUYkSDJpHug-vfC5S-NXRA?sub_confirmation=1 Follow me on Insta https://www.instagram.com/selenium_express/

Comment