Analysis of inheritance relationship design in Hibernate framework of Java

This time, let's talk about the hierarchical design of hibernate, which is the design of inheritance relationship between entities. Maybe it's more abstract. Let's look at the example directly. 1) Let's take a look at the common way to directly Code: the three real classes are as follows:

Here we need three mapping files, as follows:

A very common mapping file, which is no different from the previous one. Let's write a test method directly:

Note that we use the TItem class instead of the specific word class. Here, it will automatically find the subclasses inherited from the TItem class and find out all the results. This involves a polymorphic pattern. The class tag has the attribute polymorphism, and its default value is implicit, which means that you can query the results without specifying a name. If it is explicit, it indicates that you need to specify a specific class name before you can find the results of this class. 2) In the last example, we used three mapping files. When we need to modify, we need to modify three mapping files, which is not feasible for large projects. Moreover, each table has the corresponding field of the corresponding main class, which is redundant. So we have the following method. The entity class is the same as in 1). We changed the mapping file from three to one, leaving only the TItem mapping file. However, we need to make corresponding modifications. Now the contents are as follows:

Here, we only have one mapping file, but there is a joined subclass tag, which indicates that this class inherits from the current class, < key > indicates the primary key of the sub table. Here, the sub table refers to the two tables corresponding to the sub class, tbook and tdvd. Only fields in the sub table are specified in property. In this way, the table generated after running is as follows:

The tables corresponding to the two subclasses only have the fields specified through property. In this way, multiple fields in the table are avoided, so that the word table only maintains its individual fields. When the item class is changed, there is no need to modify too much.

3) Let's learn another way to realize hierarchical design, which is realized by embedding flags in the table. In the mapping file of hibernate, we implement it through the descriptor tag. Without much nonsense, let's look at the example directly: We modified the mapping file of TItem yesterday as:

In the middle, we added a discriminator tag, which indicates which field we use to distinguish the following two subclasses.

We see these two paragraphs. It indicates that when the value of the field specified by the discriminator is 1, it indicates that it is a tbook class and pagecount has a value; When the field value specified by the discriminator is 2, it indicates that it is a tdvd class and the regioncode has a value. In this way, we only need to use one table to show the relationship between several classes. Note that this method is not good for the case of too many subclasses. It will cause too many fields in the main table and cause some design inconvenience.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>