CREATION OF THE CLIENTI ENTITY
Entity classes are the schematic representation of the tables we are going to modify or read in our relational database. They are similar to the previously created domain classes, however, they have a different behavior. The first step is to create the package where Hibernate will look for them. Before creating the class let’s look in our relational database at the tables involved.
THE CLIENTI TABLE
Let’s look at the structure of the CLIENTI table.
As can be seen, CODFIDELITY is the primary key. A primary key uniquely identifies a row in the table; no two rows can exist with the same code. Now let’s create the entity class CLIENTI. The first important notation to specify is @Entity. The second is @Table, optional and is used to reference the relational database table. It requires a parameter, the name that identifies the table. The moment we give our class the same name as the table then we do not need this notation. We now enter the private fields that must be consistent with the database fields. The @Id notation specifies that the field will be the primary key of our table. Hibernate will not accept an entity class without this notation. Having done this we insert another notation @Column that identifies in its name parameter the column of the Clienti table. Again if the private field name is the same as the table name, this notation is optional. When we are dealing with a field of type date we need to insert the @Temporal notation.
CREATION OF RELATIONSHIPS BETWEEN CLIENTI AND CARDS
The relationship existing between CLIENTI and CARDS is ti type one-to-one, that is, one record in the CLIENTI table corresponds to one record in the CARDS table.
Same thing applies between the CLIENTI table and the UTENTI table, the relationship is one-to-one. Conversely, the relationship between the table UTENTI and PROFILI is one-to-many, the same user may correspond to several profiles. Now we need to modify our entity classes so that they are consistent with these types of relationships. We modify the Clienti class by adding the one-to-one relationship with the CARDS table, this means that when we display the Clienti table we will also display the CARDS. We insert a private Card field and a new notation @OneToOne that specifies a one-to-one relationship, in this notation we need to specify the FetchType, we have two possible options, LAZY and EAGER. With LAZY the data will be loaded only when needed, while with EAGER the data from the related table will always be loaded.
LAZY AND EAGER
With the LAZY we have better performance as the memory required is less than with the EAGER. However, when we need, for example, to load data into a combo of a related table, the choice must be EAGER. We also need to include another very important notation @PrimaryKeyJoinColumn, which indicates that the link will be via primary key. Once we have modified the Entity Clienti we must also modify the Entity Cards. We create a new private Clienti field and insert the notation @OneToOne, but this time with the parameter mappedBy=”card,” this means that the join point is the private card field in the Clienti Entity.
CREATING THE RELATIONSHIPS BETWEEN THE OTHER ENTITIES
The CARDS table is a statistical table and cannot be changed, while the UTENTI table can. When specifying the relationship between two tables that can be edited, there is not only a matter of selection, but also of insertion, update, and deletion. The CascadeType parameter given on the Utenti private field in the Clienti Entity indicates which operations are to be cascaded. ALL means all possible operations, The OrphanRemoval parameter set to true means that the moment we delete a record from the CLIENTI table, the remaining orphaned records in the UTENTI table will also be removed. The last relationship we are going to create is between the UTENTI table and the PROFILI table, which is of the one-to-many type. Let’s go to the Utenti entity, we said that one user can correspond to multiple profili, so our private field will be:
private Set<Profili> profili = new HashSet<>();
The notation to use is the @OneToMany, the other options we already know.
INTRODUCTION TO CREATING QUERIES WITH JPA 2
In the case of entities we have two possible alternatives for generating queries.
JPQL allows queries to be placed directly on entities. The Criteria API uses a whole series of methods that are applied to entities.
On the surface it looks very complex, but when we use it we will see that it is quite simple to use the criteria API. Let us look at an example of the other type, namely JPQL.
DOWNLOAD ARTICLE CODE
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.
Leave A Comment