- Hibernate - Interceptors
- Hibernate - Batch Processing
- Hibernate - Caching
- Hibernate - Native SQL
- Hibernate - Criteria Queries
- Hibernate - Query Language
- Hibernate - Annotations
- Hibernate - O/R Mappings
- Hibernate - Examples
- Hibernate - Mapping Types
- Hibernate - Mapping Files
- Hibernate - Persistent Class
- Hibernate - Sessions
- Hibernate - Configuration
- Hibernate - Environment
- Hibernate - Architecture
- Hibernate - Overview
- ORM - Overview
- Hibernate - Home
Hibernate Useful Resources
- Hibernate - Discussion
- Hibernate - Useful Resources
- Hibernate - Quick Guide
- Hibernate - Questions and Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Hibernate - O/R Mappings
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
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 | This is mapped with a <set> element and initiapzed with java.util.HashSet |
2 | This is mapped with a <set> element and initiapzed with java.util.TreeSet. The sort attribute can be set to either a comparator or natural ordering. |
3 | This is mapped with a <pst> element and initiapzed with java.util.ArrayList |
4 | This is mapped with a <bag> or <ibag> element and initiapzed with java.util.ArrayList |
5 | This is mapped with a <map> element and initiapzed with java.util.HashMap |
6 | This is mapped with a <map> element and initiapzed with java.util.TreeMap. The sort attribute can be set to either a comparator or natural ordering. |
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.
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
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 cardinapty 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 | Mapping many-to-one relationship using Hibernate |
2 | Mapping one-to-one relationship using Hibernate |
3 | Mapping one-to-many relationship using Hibernate |
4 | Mapping many-to-many relationship using Hibernate |
Component Mappings
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 pfe cycle and completely depends on the pfe 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 | Mapping for a class having a reference to another class as a member variable. |