4.Hibernate O/R mapping

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.