springHEREDITY OF ENTITY CLASSES

From a technical point of view, entity classes are for all intents and purposes Java classes that therefore follow the identical rules of any other class. Consequently, entity classes are also subject to inheritance; therefore, they can edit properties that come from superclasses. Let’s look at the classes Clienti Cards and Utenti, these three classes have the same primary key. One could create a perhaps abstract class that implements CODFIDELITY and inherit it in the above classes. We create a new abstract class AbstractEntityClienti. Abstract classes defined as entities need a specific notation at the top @MappedSuperclass. The access modifier is protected and this is normal because derived classes will need access to this property. Once this abstract class is implemented, we can modify the other three classes by removing the private codFidelity field and their respective getters and setters by implementing the newly created superclass.

Classe astratta

THE @ATTRIBUTEOVERRIDE NOTATION

Let’s look at a somewhat more complex situation than the previous one by introducing a new table, the Premi table.

Premi

We immediately notice that the field name, primary key, in the Premi table is Fidelity, so only the name is different, but the type is the same. Let us now see what happens if we create the Premi entity and derive it from AbstractEntityClienti. If we try to start the application we get a SQL Server error because the column name was not respected. Let’s see how this situation is handled. We go to the Premi table and use a new notation, @AttributeOverride. The @Basic notation is a Hibernate alternative to what we have seen so far.

Table Premi

ENRICH ENTITY CLASSES WITH @TRANSIENT NOTATION

Up to this point, all fields in our entities are consistent with those in their respective tables. There are situations where we need fields that are not in the respective table, then to tell Hibernate that a particular field is not in the table, therefore, it will not be subject to persistence we use the @Transient notation. We create a new “nominativo” field in the Clienti entity that will replace the first and last name. In the getter of this new unmapped field, we are going to add the first and last name separated by a space. Now we can edit the clienti.jsp view using the nominativo.

Transient

MAPPING ENUMERATORS

The enumeration we will consider is the“stato” field of the CLIENTI table. It can take two values, 1 active state, 0 suspended so it cannot receive fidelity points. We create a new enumerator in our Java project called Stato, and change the private Stato field of the Clienti entity to the new enumerator.

Ordinal means that the enumeration will start from zero and go in ascending order. All that remains is to edit the inscliente and clienti views.

Stato
Clienti

MAPPING LOB (LARGE OBJECT)

You have this type when, for example, we need to insert images into the database.

LOB

There is the possibility within a database relate save for example the bytes of an image or any other type of file. The FetchType that we use with a LOB is the LAZY, this is very important because being fields that are very heavy in nature, we are only going to read the data as needed.

LOB

DOWNLOAD ARTICLE CODE

AlphaShopV6

AlphaShopV7

The AlphaShopV6.zip project is for the SQL Server DBMS while AlphaShopV7.zip is for MySQL.

THE JAVA LANGUAGE

THE JAVA LANGUAGE

LINKS TO PREVIOUS POSTS

SPRING FRAMEWORK