1.1 Mapping classes to tables
After learning JDBC, you most likely have the impression that working with a database from a Java application is still a pleasure. What if I told you that all this work could be done 10 times easier?
What is the main advantage of the SQL language? This is a declarative language - it describes what we want to get, and says nothing at all about how to do it. How - this is the concern of the SQL server.
The same approach can be used when working with databases.
In an ideal world, we could simply write SQL queries to the database, and in response we would receive ready-made Java objects, or collections of Java objects, if we requested several pieces.
What can I say, this is what several guys thought in 2000 and decided to write their own ORM framework.
ORM stands for Object-Relational Mapping and is essentially a mapping of Java objects to SQL queries.
The guys came up with a very simple thing - each table in the database must correspond to some class in the Java application . In a Java application, we operate with objects, and these objects already know how to save themselves to the database.
There were three approaches to solving this problem, and they looked something like this:
- The object saves itself to the database and updates its fields based on information from the database.
- The object is able to save itself to the database, but never initiates this case.
- The object contains only data, and someone saves it to the database and loads it from the database.
Initially, the first approach dominated, then Application servers and Enterprise Java Beans were popular. There was even a whole class of beans called Persistence EJBs that could save themselves into the database themselves.
But one day everything changed...
1.2 Emergence of Hibernate
In 2001, the first version of the Hibernate framework was released. It was a very simple framework, but it allowed the use of ordinary "stupid objects" that did not know anything about how they should be stored in the database or loaded from there.
Mapping of fields of Java classes and columns in a table in the database was set using an XML file. And sometimes they were quite bulky. Okay, who am I kidding. They were hefty canvases of XML code. And the situation was saved only by the fact that 20 years ago there were no such gigantic databases as now.
But in fact, the most powerful decision was to finally separate the object that needs to be saved to the database from the code that saved it there . This solution is not really obvious. Because the principle of encapsulation states that the object knows best about how it needs to be saved and loaded.
And the ORM approach really breaks that concept. The data class exposes its internal structure, but it has become much easier to operate with groups of objects of different types.
A major breakthrough came after the release of Java 5 , when two things appeared in the JDK:
AnnotationsXML was quickly supplanted, and now it was easy to specify all the necessary settings for mapping a Java class to a table in the database right in the Java class.
Proxynot so noticeable to the Hibernate user, but their contribution was even more serious. When you request a specific object or objects from Hibernate, it simply returns you a stub (proxy), and intercepts all calls to its methods.
This made it possible to implement various Lazy Loading mechanisms and raised the speed and efficiency of Hibernate to a completely sky-high level for that time. Hibernate has become not just a de facto industry standard - it has begun to be translated into other languages. So, for example, Framework NHibernate appeared for C#.
1.3 Emergence of JPA
De facto followed by de jure recognition. The JDK developers decided to create a specification on how to correctly map objects to tables in a database. This specification is calledJPA- Java Persistence API.
This is exactly the specification. It describes how everything should work and what annotations we need to mark different parts of the class if we want its objects to be saved to the database.
It seems that the guys just took Hibernate as a basis and changed the package names from it. Because all the annotations that were in Hibernate moved to JPA almost one by one.
Today, Hibernate fully implements the entire JPA specification, as well as some additional features that make working with it even more comfortable. Therefore, in terms of standardization, we can say that Hibernate has two sets of features:
- JPA standard
- Hibernate Native API (additional functionality)
The official Hibernate documentation describes it like this:
But both based on my experience and after re-reading the Hibernate documentation, I can say that JPA and Hibernate API are 95% the same. They are just identical concepts.
1.4 Maven for Hibernate
Since I've praised Hibernate so much, I think it's time to move on to working with it a little harder.
Firstly, there is an official site, where there is just a bunch of English-language documentation. She, of course, has a bias in reference information, and not in training. But it's still better than debugging the sources, right? :)
- You open the link .
- You look at her for a long time.
- Coming back to CodeGym.
- You read my further lectures.
My job is to simplify complex things and explain them in simple terms. And if you have reached this level, then I can do it.
Well, to get started with Hibernate, you need to add it to your pom.xml. To date, the 6th version of Hibernate is already available, or rather 6.1.1, so we will learn how to work with the latest version.
Just add these lines to your pom.xml:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>6.1.1.Final</version> </dependency>
If you are reading this lecture outside the window of 2023+, then the new version can be downloaded here .
Important! Some of the libraries that Hibernate uses have been deprecated in JDK 11 and JDK 17, so if you're having trouble getting your project up and running, add these dependencies to it:
<dependency> <groupId>jakarta.xml.bind</groupId> <artifactId>jakarta.xml.bind-api</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.29.0-GA</version> </dependency>