O/R mappings
So far we've seen a very basic O/R mapping by applying Hibernate, but there are three more important topics about mapping that need to be explored in more detail. These three topics are collection mappings, association mappings between entity classes, and component mappings.
Collection Mappings
If an instance or class has a collection of values for a particular variable, then we can apply any of the available interfaces in Java to map the values. Hibernate can save java.util.Map, Java.util.Set, Java.util.SortedMap, Java.util.SortedSet, Java.util.List and other instances of any array of persisted instances or values.
| collection type |
Mapping and Description |
| Java.util.Set |
It matches the \<set> element and initializes with Java.util.HashSet. |
| Java.util.SortedSet |
It matches the \<set> element and initializes with Java.util.TreeSet. The sort property can be set either as a comparator or as a natural sort. |
| Java.util.List |
It matches the \<list> element and initializes with Java.util.ArrayList. |
| Java.util.Collection |
It matches \<bag> or \<ibag> elements and initializes with java.util.ArrayList. |
| Java.util.Map |
It matches the \<map> element and initializes with Java.util.HashMap. |
| java.util.SortedMap") |
It matches the \<map> element and initializes with Java.util.TreeMap. The sort property can be set either as a comparator or as a natural sort. |
For Java's original numeric value hibernate takes <primitive-array> an array of support, and for other values in Java hibernate takes <array> an array of support. However, they are seldom applied, so I will not discuss them in this guide.
If you want to map a user-defined collection interface and this interface is not directly supported by hibernate, then you need to tell Hibernate the syntax of the set that you define, which is difficult to manipulate and is not recommended for use.
Association Mappings
The Association mappings between entity classes and the relationships between tables are the soul of ORM. A subset of the relationships between objects can be interpreted in the following four ways. Association mappings can be one-way or bidirectional.
| Mapping Type |
Description |
| Many-to-one |
Using Hibernate to map a many-to-one relationship |
| One-to-one |
Mapping a one-to-one relationship using Hibernate |
| One-to-many |
Mapping a one-to-many relationship using Hibernate |
| Many-to-many |
Using Hibernate to map many-to-many relationships |
Component Mappings
A member entity class as a variable is likely to have a correlation with other classes. If the referenced class does not have its own life cycle and relies entirely on the life cycle of the entity class that owns it, then this reference class can be called a component class.
The mapping of a component collection is likely to be similar to a normal set of mappings, with little difference in settings. We can look at both of these mappings in the example.
| Mapping Type |
Description |
| Component Mappings |
The mapping of a class has a reference function for another class that is a member of a variable. |
_______________________________________________________________________________________________________________ __
4.Hibernate O/R mapping