Object-oriented and relational databases
The Java language is inherently an object-oriented programming language, in the Java world, the processed content is organized into an object, there are inheritance, reference relationships between objects and objects, such a relationship cannot be directly mapped to a relational database in a simple way. Therefore, there is impedance detuning between relational database and object-oriented object.
We use a simple example to illustrate the difficulty that this impedance mismatch poses to enterprise application developers. Suppose there are three Java classes in an enterprise application: Animal, Fish, and Dog. Animal has only two attributes: ID and name. Fish is a kind of Animal, but people pay more attention to its life area is in the sea or in the river, so it inherits from Animal, but also has its own unique attribute territory. Dog is also a kind of Animal, people pay more attention to its gender, so it inherits from Animal, but also has its own unique attribute sex. We can use the following diagram to describe the relationship model between the three.
Figure 1. Animal, Fish, and Dog object models
In Java applications, Fish and Dog can be treated as Animal objects because of the support of dynamic bindings. But if we switch to a relational database perspective, things change: Normally, Animal, fish, Dog have nothing to do with each other, and they are all kept in their respective tables, assuming they correspond to Animal tables, fish tables, and DOG tables, if you want to maintain Animal and fish inheritance Relationship, we need to use SQL's federated query statement to detect all the attributes of Animal and all of Fish's properties, so that we have to use some foreign key to associate:
Select animal.*,fish.* form animal,fish where animal.id = Fish.id
From this simple example, we can see that when an enterprise application developer uses Java to implement enterprise application, it needs to master two kinds of ideas of object-oriented and relational database, and it must ensure that the mapping between them is correct, otherwise it can't guarantee the correctness of enterprise application, which is for enterprise application development is a challenge, the Java community has been looking for a simple way to unify object-oriented and relational database ideas, and this effort has contributed to the development of sustainable technologies.
OpenJPA is the latest attempt to make the persistence of object inheritance transparent, and enterprise application developers only need to deal with the object model without having to deal with content related to relational database, which greatly reduces the difficulty of persistence of object inheritance relationship. Let's look at several ways to persist object inheritance relationships in OpenJPA.