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

NATIVE HIBERNATE CONFIGURATION

  • First, we use the tags to indicate the fact that we are configuring Hibernate #1, more exactly the SessionFactory object #2. SessionFactory is an interface and we need one SessionFactory to interact with one database.
  • We indicate the JDBC properties — driver #3, URL of the database #4, username #5, and password #6 to access it.
  • We limit the number of connections waiting in the Hibernate database connection pool to 50 #7.
  • Every time the program is executed, the database will be created from scratch #8. This is ideal for automated testing when we want to work with a clean database for every test run.
  • To create a SessionFactory, we need to create a Configuration #1, to call the configure method on it, and to add Item to it as annotated class #2. The execution of the configure method will load the content of the default hibernate.cfg.xml file
  • We create and configure the service registry #3. A ServiceRegistry hosts and manages services that need access to the SessionFactory
  • We build a SessionFactory using the configuration and the service registry we have previously created #4
  • The SessionFactory created with the createSessionFactory method we have previously defined is passed as an argument to a try with resources, as SessionFactory implements the AutoCloseable interface #5. Similarly, we begin a new session with the database by creating a Session #6, which also implements the AutoCloseable interface. This is our context for all persistence operations
  • Get access to the standard transaction API and begin a transaction on this thread of execution #7
  • Create a new instance of Item class, and set its info property #8
  • The transient instance becomes persistent on the side of the persistence context #9
  • Synchronize the session with the database and close the current session on commit of the transaction automatically #10
  • As in the case of JPA, there is no SQL code and no JDBC usage. There are no CRUD (create, read, update, delete) operations inside the Java code, but only working object-oriented way, with classes, objects, and methods. The translation to each SQL dialect is made by the ORM which also addresses portability

--

--

--

https://www.luxoft-training.com/

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

Recommended from Medium

Save Life using GPS and IoT for the Traveler During Accident.

Builder Design Pattern in Swift and iOS/macOS Universe

Get Your Own Data — Building a Scalable Web-Scraper with AWS

How to Deploy a Fedora Linux VM Instance in GCP using Terraform

Building something that matters

Cranes Guide — Your Simple Choice

AWS RDS(MS SQL Server) Read replica migration

The Audacity!

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

https://www.luxoft-training.com/

More from Medium

Tree Entity with JPA and spring-boot

Writing a Logging Aspect with Spring AOP

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

Getting Started with Apache Kafka and Spring Boot