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

Mixed Content: The page at xxx was loaded over HTTPS, but requested an insecure yyy

My thoughts on Digitization, Digitalization and Digital Transformation

[Paper notes] Bigtable: A Distributed Storage System for Structured Data

Python Foundation — Immutable types

How do I learn coding completely beginner with no experience in computers?

Web, Widget, Extension Help Centers

RetroDEFI X FarmerDOGE

A Comparison Between Scrum and Extreme Programming

Four hands on keyboard

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. Native Hibernate configuration

Lets create a java sample project part 1

Java Date Time data types

How to use Spring Cache with Hazelcast?