2. Introduction to Hibernate
1. Hibernate
Hibernate is an ORM framework for Java, which facilitates the mapping of attributes between a relational database and the object model of our application through XML files or annotations in the entity beans. It is free software distributed under the GPL 2.0 license, so it can be used in commercial applications.
The main function of Hibernate will be to offer the programmer the tools to detail his data model and the relationships between them, so that it is the ORM itself that interacts with the database, while the developer is dedicated to manipulating objects.
In addition, it offers a query language, called HQL (Hibernate Query Language), so that it is the ORM itself that translates this language to that of each database engine, thus maintaining portability at the expense of a slight increase in time of execution.
When objects are created, they are volatile or transient (will be destructed when the application finish). When we want to store them with Hibernate, they are tracked with one mechanism called Session. The objects loaded from databases are tracked too. If we want, we could finish the tracking, deleting the object when we do not need more.
Moreover, a query language is provided, HQL or Hibernate Query Language, based on OQL. The underlying part of the session, as can be seen, allows the use of various technologies, including JDBC to connect to the necessary DBMS.
2. Configuration
Hibernate, as a good framework does not need an installation, because is integrated in our project as libraries. We could choose to install lots of jar libraries, but is pretty good to use a package manager to automate this task. The building process of the project will be easier at the end.
We will use Maven as package manager.
3. Dependencies.
In our projects two basic tools will be used: Hibernate and a driver to connect to the chosen database. Obviously, we need to add the dependencies to the package manager. In maven, the dependencies are included in the Pom.xml file, in the root folder of our project. Inside de <dependencies> tag you must add:
If you have chosen Gradle as package manager, your build.gradle set as:
Remember
Remember that you can find the packages in https://mvnrepository.com/repos/central
4. Project structure
Once we have added dependencies, we must create a project structure to organize our classes, in order to separate the logic program. We will show a brief description, getting a deeper explanation later.
4.1. Beans
Beans are the evolution of POJOs, that we have studied in previous units. Remember that these classes are common objects, out of inheritance trees and without implement any interfaces. They are used to storing information about one concept (I need a Car...create a Car).
As an extension of POJO's, Beans appear, without restrictions in attributes, constructors and inheritance. They have some restrictions:
- Its attributes must be
private(encapsulation). - They must implement
serializableinterface. - They must have public getters and setters.
- They must implement a generic constructor (without arguments).
Remember
The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization.
If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named serialVersionUID that must be static, final, and of type long:
| Java | |
|---|---|
In summary, Beans are data access components, and they represent entities in our application. It is a good idea to create our Beans in the same package, normally called Model.
Tip
Remember Lombok library? It is very useful to create our beans with only a few lines of code.
4.2. Mapping files
Once entities are created, we need to map each entity. Each mapping sets the references between beans and tables, attributes and columns, in order to establish a perfect match between them.
First option will be creating mapping files, with the syntax (in XML) between classes and tables. If the bean is called Car, the mapping file should be Car.hbm.xml:
hbmis for hibernate mappingxmlbecause syntax is in XML specification.
We will study mapping files in next sections.
4.3. Other files
Finally, we will have the rest of the classes of the program, as well as the application or main class. If you are designing an application with a graphical environment, there would be data representation classes or views. Similarly, in case of a web application, controllers and services would be missing.
5. Project Configuration
Let's take a closer look at the Hibernate configuration file. In the Hibernate configuration file we can set the options in a disorderly way, but it is recommended to group the option blocks for clarity and maintenance, as well as to indicate through comments what we are doing at that moment. We will see it with a complete example, which we will detail little by little in the following sections.
5.1. Hibernate.cfg.xml
Notice than:
- Show SQL queries option is high recommended to have it set to
true, at least in first projects, to see how the mapping of objects to SQL queries actually occurs. - The
hbm2ddloption is very powerful, since if we only start from the object-oriented model, Hibernate will create the database for us (obviously empty of data). We will see in a later practice another very interestinghbm2javawill appear, which through reverse engineering will allow us to create our Beans from the relational design. - XML mapping files (
) must be together with the java classes, in the same package. - Mappings within the classes (
) refer to the Beans themselves, as we will see in next section.
5.2. Loading scripts
If you want to insert some data in your database test applications, it is a good idea to have a SQL script and load it automatically when hibernate configuration is loaded. You can add a file under src/main/resources called import.sql, and, after table creation, if exists, this script will be executed. This is the way to start a project with existing test data in our databases.
Moreover, if you want to execute more scripts, you can add specific files in hibernate.cfg.xml, as follows:
6. Loading configuration and Sessions
In order to load the previous configuration file, we must create a SessionFactory object throw we can create Session instances to connect to our database. The structure of this class will be always the same: load the configuration xml file, and then create the SessionFactory.
Attention
This implementation has been done with Singleton design pattern, in order to have a unique instance of SessionFactory