JPA AND HIBERNATE

springThe objects of the next articles will be two very important frameworks involving the persistence layer. JPA and Hibernate. Hibernate ORM (or simply Hibernate) is an object-relational mapping tool for the Java programming language. Provides a framework for mapping an object-oriented domain model to a relational database. Sostitutes direct and persistent database accesses with high-level object management functions. Hibernate is free software distributed under the GNU Lesser General Public License 2.1.The main feature of Hibernate is mapping from Java classes to database tables and mapping from Java data types to SQL data types. Hibernate also provides query and data retrieval capabilities. It generates SQL calls and relieves the developer from manual handling and conversion of result set objects.

Introduzione
Hibernate JPA

Let us look at the working scheme of Hibernate. We start with a Java POJO (Plain Old Java Object) class , the data travels via the JPA and Hibernate into the relational database. The JDBC we have used so far is not completely set aside, but it is still needed to establish connections to the various DBMSs.

Hibernate e JPA

POJOs are named entities and represent the transposition of relational database tables into Java classes, or queries, as we will have a chance to see we can create entities that are based on queries. We have seen that using the JDBC driver, T-SQL code and Store Procedure produce the desired effect, so why introduce two new Frameworks? Let’s see what the advantages are.

Vantaggi di Hibernate

HIBERNATE AND SECURITY

In addition to these benefits we must add the element of security, Hibernate is not prone to Sql Injection attacks. This is because you have no SQL code, or rather the SQL code is generated by Hibernate behind the scenes.

EDIT POM.XML

POM.xml

THE GOALS OF THIS SECTION

We will have way to add customer management, we will make sure that we can configure a user to access our database by his password, or each user can be associated with one or more profiles. We said that ORMs simplify operations in our database, the tables involved are those shown in the schema. Although ORMs simplify database management it is always a good idea to analyze them, this is for two reasons, the first being that entities will represent tables and their relationships, and secondly it is essential to know the characteristics of the database you are modifying.

THE RELATIONSHIPS BETWEEN THE TABLES INVOLVED

As you see from the diagram shown, the relationship between the CLIENTI table and the UTENTI table is one-to-one, that is, a customer has only one row associated with it in the UTENTI table. Users are those who are interested in entering our site, but there may also be customers who are not interested in entering the site, and therefore will not have a row in the UTENTI table. In contrast, the relationship between the table UTENTI and PROFILI is one-to-many, that is, one user may be associated with multiple profiles.

Tabelle

CREATION OF HIBERNATE CONFIGURATION JAVA CLASSES

Now it is time to create the configuration class, which will allow Hibernate to connect to our data source. The connection is always made through the JDBC. We create a new class HibernateConfig.java. As you have seen us do several times at the top of the class we insert the notation @Configuration, through this notation the IOC will load the class at application startup. The second notation is @EnableTransactionManagement, meaning that all activities performed on the relational database will have the transaction mechanism enabled.

TRANSACTIONS

If the operation is carried out in multiple steps if one of them fails all the others will be rolled back. In short, all changes made by previous phases will be undone. Otherwise, i.e., there are no errors the transaction will be committed. All this is handled automatically by Hibernate. The third notation we know is the @ComponentScan that will tell Hibernate where to go to look for configuration files. The last one the @PropertySource will tell Hibernate where to go to look for all the configuration variables we have entered. The first line of code we Autowired is the Environment class, this means that we will access the configuration specifications written in application.properties through this class. See the file.

application.properties

The other class we code injected is the DataSource specified in the JdbcConfig class. This is, and I reiterate, because Hibernate uses JDBC to connect to the database. We also need to disable the transaction handling that the JDBC used to handle since Hibernate will handle them. The first Bean we will create is the most important as it handles the Entity Manager. The code is explained step by step, and you can consult it by downloading the project of interest to you.

HibernateConfig

DOWNLOAD ARTICLE CODE

AlphaShopV6

AlphaShopV7

The AlphaShopV6.zip project is for the SQL Server DBMS while AlphaShopV7.zip is for MySQL. I recommend you try using the Intellij IDEA editor, it is a very good editor full of features including a very powerful tool like AI.

THE JAVA LANGUAGE

THE JAVA LANGUAGE

LINKS TO PREVIOUS POSTS

SPRING FRAMEWORK