Object-relational Mapping Using JPA Hibernate and Spring Data JPA. Persistence with Spring Data JPA

Persistence with Spring Data JPA

  • The @EnableJpaRepositories annotation will scan the package of the annotated configuration class for Spring Data repositories #1.
  • We create a data source bean #2 and we indicate the JDBC properties — driver #3, URL of the database #4, the username #5, and password #6 to access it.
  • We create a transaction manager bean based on an entity manager factory #7. Every interaction with the database should occur within transaction boundaries and Spring Data needs a transaction manager bean.
  • We create a JPA vendor adapter bean that is needed by JPA to interact with Hibernate #8. We configure this vendor adapter to access a MySQL database #9.
  • We create an object belonging to the class LocalContainerEntityManagerFactoryBean– this is a factory bean that produces an EntityManagerFactory following the JPA standard container bootstrap contract #10.
  • We set the data source #11, the vendor adapter #12, and the packages to scan for entity classes #13. As the Item entity is located in cscs23.orm, we set this package to be scanned.
  • Spring Data JPA provides support for JPA-based data access layers reducing the boilerplate code and creating implementations for the repository interfaces. We need only to define our own repository interface, to extend one of the Spring
  • Data interfaces.
  • We extend the test using SpringExtension #1. This extension is used to integrate the Spring test context with the JUnit 5 Jupiter test.
  • The Spring test context is configured using the beans defined in the previously presented SpringDataConfiguration class #2.
  • An ItemRepository bean is injected by Spring through auto-wiring #3. This is possible as the cscs23.orm.repositories package where ItemRepository is located was used as the argument of the @EnableJpaRepositories annotation in listing 10.
  • If we would like to check and call itemRepository.getClass(), we’ll see that it is something like com.sun.proxy.$Proxy41 — a proxy generated by Spring Data JPA, as explained in figure 1.
  • Create a new instance of the Item class, and set its info property #4.
  • Persist the item object #5. The save method is inherited from the CrudRepository interface and its body will be generated by Spring Data JPA when the proxy class is created. It will simply save an Item entity to the database.
  • The advantages of working with JPA or Hibernate native are still here: no SQL code or JDBC needed; no CRUD operations inside the Java code, but only classes, objects, and methods; the portability to each SQL dialect is addressed by the ORM.
  • Additionally, the Spring Data JPA test is considerably shorter than the ones using JPA or Hibernate native. This is because the boilerplate code has been removed — no more explicit object creation or explicit control of the transactions. The repository object is injected and it provides the generated methods of the proxy class. The burden is heavier now on the configuration side — but this should be done only once per application.





Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Importing data from MySql to Hive using Sqoop

ESP32 Covid19 Tracker Project

What I learnt completing IBM Applied AI professional certificate on Coursera.

Getting Started in GraphQL With Spring Boot in Java

The Future of IT: Autonomous Data Services

AutoAmplify Partnership Programme

App Highlights: Airtable

New Writer on the Block

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Luxoft Training

Luxoft Training


More from Medium

Object-relational Mapping Using JPA, Hibernate and Spring Data JPA.

Profile Java Applications using VisualVM

Enable authentication and configure ActiveMQ in Java application

What is Spring Framework & IoC (Inversion Of Control) in Spring?