May 17, 2021 Hibernate
So far we've seen very basic O/R mapping by applying Hibernate, but there are three more important topics related to mapping that we need to explore in more detail. These three topics are the mapping of collections, the association mapping between entity classes, and the component mapping.
If an instance or a class has a collection of values for a particular variable, we can apply any of the available interfaces in Java to map those values. Hibernate can save instances of java.util.Map, java.util.Set, java.util.SortedMap, java.util.SortedSet, java.util.List, and any array of other persistent instances or values.
The collection type | Mapping and description |
---|---|
java.util.Set | It matches the element and is initialized with java.util.HashSet. |
java.util.SortedSet | It matches the element and is initialized with java.util.TreeSet. The sort property can be set as a comparator or sorted naturally. |
java.util.List | It matches the element and is initialized with java.util.ArrayList. |
java.util.Collection | It matches the elements and initializes them with java.util.ArrayList. |
java.util.Map | It matches the element and is initialized with java.util.HashMap. |
java.util.SortedMap ") | It matches the element and is initialized with java.util.TreeMap. The sort property can be set as a comparator or naturally sorted. |
For the original values of
<primitive-array>
Java, Hibernate is supported by an array, and for the other values of Java, Hibernate is
<array>
by an array of supported by a support array.
However, they are rarely used, so I will not discuss them in this guide.
If you want to map a user-defined collection interface that is not directly supported by Hibernate, then you need to tell Hibernate the syntax of the collection that you define, which is difficult to operate and not recommended for use.
The association mapping between entity classes and the relationships between tables are the souls of ORM. A subset of the relationships between objects can be interpreted in four ways. Association maps can be one-way or two-way.
The type of map | Describe |
---|---|
Many-to-One | Use Hibernate to map a many-to-one relationship |
One-to-One | Use Hibernate to map one-to-one relationships |
One-to-Many | Use Hibernate to map one-to-many relationships |
Many-to-Many | Use Hibernate to map many-to-many relationships |
Entity classes that are part of variables are likely to be related to other classes. If the referenced class does not have its own lifecycle and relies entirely on the lifecycle of the entity class that owns it, the reference class can therefore be called a component class.
The mapping of component collections is likely to be similar to that of normal collections, with only a few differences in settings. We can look at these two maps in the example.
The type of map | Describe |
---|---|
Component Mappings | The mapping of classes is a reference for additional classes that are part of a variable. |