Hibernate 简明教程
Hibernate - O/R Mappings
到目前为止,我们已经看到了使用 hibernate 的非常基本的 O/R 映射,但还有三个非常重要的映射主题,我们必须详细了解。
So far, we have seen very basic O/R mapping using hibernate, but there are three most important mapping topics, which we have to learn in detail.
它们是:
These are −
-
Mapping of collections,
-
Mapping of associations between entity classes, and
-
Component Mappings.
Collections Mappings
如果实体或类具有针对特定变量的值集合,那么我们可以使用 java 中可用的任何一个集合接口来映射这些值。Hibernate 可以保留 java.util.Map, java.util.Set, java.util.SortedMap, java.util.SortedSet, java.util.List 和任何持久实体或值 array 的实例。
If an entity or class has collection of values for a particular variable, then we can map those values using any one of the collection interfaces available in java. Hibernate can persist instances of java.util.Map, java.util.Set, java.util.SortedMap, java.util.SortedSet, java.util.List, and any array of persistent entities or values.
Sr.No. |
Collection type & Mapping Description |
1 |
java.util.SetThis is mapped with a <set> element and initialized with java.util.HashSet |
2 |
java.util.SortedSetThis is mapped with a <set> element and initialized with java.util.TreeSet. The sort attribute can be set to either a comparator or natural ordering. |
3 |
java.util.ListThis is mapped with a <list> element and initialized with java.util.ArrayList |
4 |
java.util.CollectionThis is mapped with a <bag> or <ibag> element and initialized with java.util.ArrayList |
5 |
java.util.MapThis is mapped with a <map> element and initialized with java.util.HashMap |
6 |
java.util.SortedMapThis is mapped with a <map> element and initialized with java.util.TreeMap. The sort attribute can be set to either a comparator or natural ordering. |
Hibernate 使用 <primitive-array>(针对 Java 原始值类型)和 <array>(针对所有其他项)支持数组。但是,它们的使用频率很低,因此我不会在本教程中对它们进行讨论。
Arrays are supported by Hibernate with <primitive-array> for Java primitive value types and <array> for everything else. However, they are rarely used, so I am not going to discuss them in this tutorial.
如果您想映射 Hibernate 不直接支持的用户自定义集合接口,您需要告知 Hibernate 您自定义集合的语义,但要做到这一点并不容易,也不建议使用。
If you want to map a user defined collection interfaces, which is not directly supported by Hibernate, you need to tell Hibernate about the semantics of your custom collections, which is not very easy and not recommend to be used.
Association Mappings
实体类之间的关联映射以及表之间的关系是 ORM 的核心。以下是可以表示对象之间关系基数的四种方式。关联映射可以是单向的,也可以是双向的。
The mapping of associations between entity classes and the relationships between tables is the soul of ORM. Following are the four ways in which the cardinality of the relationship between the objects can be expressed. An association mapping can be unidirectional as well as bidirectional.
Sr.No. |
Mapping type & Description |
1 |
Many-to-OneMapping many-to-one relationship using Hibernate |
2 |
One-to-OneMapping one-to-one relationship using Hibernate |
3 |
One-to-ManyMapping one-to-many relationship using Hibernate |
4 |
Many-to-ManyMapping many-to-many relationship using Hibernate |
Component Mappings
一个实体类很可能可以具有对另一个类的引用作为成员变量。如果被引用类没有自己的生命周期,并且完全依赖于所有实体类生命周期,那么被引用类因此而成为 Component class 。
It is very much possible that an Entity class can have a reference to another class as a member variable. If the referred class does not have its own life cycle and completely depends on the life cycle of the owning entity class, then the referred class hence therefore is called as the Component class.
以类似方式映射集合组件也是可能的,就像映射常规集合,但配置差异很小。我们将详细了解这两个映射,并提供示例。
The mapping of Collection of Components is also possible in a similar way just as the mapping of regular Collections with minor configuration differences. We will see these two mappings in detail with examples.
Sr.No. |
Mapping type & Description |
1 |
Component MappingsMapping for a class having a reference to another class as a member variable. |