THE EMBEDDED OBJECTS
In some cases the class mapping is not exactly the same as the table structure. Let us look at the Premi table where more fields have been added. These elements are typical of a master table, however, they were equally included in the table, although in such cases it is good to split the two tables. Our goal is to distinguish within the entity classes the registry part from the more informative part of the flows.
THE INFOPREMI CLASS
So we have to create another class, however this class is special, it is an embedded class called from our Premi class. I created a new InfoPremi.java class that is not an entity class. It was imported for all intents and purposes into our Premi class with the notation @Embedded, while the notation @Embeddable, in InfoPremi indicates that this class can be imported into an entity class.
Now one might ask why such a thing should be done? The answer is that sometimes tables can have many fields, then it is useful to divide them into multiple classes. From the standpoint of code order what we did is definitely more readable. @Embedded will call up all the information that is present in the InfoPremi class. Go into debug and see how the InfoPremi class has been incorporated (image below).
PRIMARY KEY GENERATION STRATEGY
The time has come to look at other types of primary key generation strategies. We will use the Coupons table to conduct tests.
The relationship as you see is one-to-many, that is, one Cliente can correspond to several coupons. We create the Coupons entity class. I created a new CouponsController, DAO classes, and Service classes.
The AUTO strategy leaves it up to Hibernate, based on the nature of the relational database, to determine the optimal strategy for assigning the primary key. When there is a one-to-many relationship one must specify the two ways, one from the Coupons side where the correlation is many-to-one specified with the @ManyToOne notation and the @JoinColumn notation.
@ManyToOne
@JoinColumn(name = “IdCliente”, referencedColumnName = “CODFIDELITY”)
private Clienti cliente;
while in the customer part the correlation is one-to-many
@OneToMany(fetch = FetchType.EAGER, mappedBy = “cliente”)
private Set coupons = new HashSet<>();
THE PRIMARY KEY TABLE GENERATION STRATEGY
We now specify TABLE as the primary key generation strategy. This means that The Id must be based on a specific table. Let’s try running the program and see what happens. We get an error because Hibernate goes to look up the following table:
Instead of leaving it to Hibernate to go and create a specific table by then enabling DDL (Data Definition Language) in the configuration, we create a table called Progressivi and instruct Hibernate to use that table.
We are telling Hibernate looks for Coupons within a specific table, Progressives. We must also specify with the @GeneratedValue notation the name of the generator that will be Coupons configured in the TableGenerator. Once this is done we run the application and paste the following address into the browser.
http://localhost:8080/AlphaShopv6/coupons/aggiungi/67301895
A new Coupons with the identifier generated through the Table strategy will be inserted. The Progressives table will also be updated. In MySQL Server I have not found the progressivi table, if it is the same for you I leave instructions for creating it and a short video for importing the two records from a .csv file available for download.
DOWNLOAD ARTICLE CODE
The AlphaShopV6.zip project is for the SQL Server DBMS while AlphaShopV7.zip is for MySQL.
Leave A Comment