Java programmers from stupid birds to cainiao () Discuss hibernate (8) hibernate set map relationship

Source: Internet
Author: User


PairIn hibernate, set attributes are very common and important in hibernate ing files. It is more important to understand and master common set attributes. In the hibernate configuration file, for example, each person's test score is a typical map structure, and each course corresponds to a score. Or a simpler set of attributes, the department of an enterprise, an enterprise usually corresponds to multiple departments. Set attributes are very common attribute relationships in real life. There are roughly two types of set attributes: the first is a set attribute, such as a list, set, or array, and the other is a set attribute of the map structure. Each attribute has a key ing.
The attributes of a set have the following elements:
(1)<Set> element: The ing type can be Java. util. attribute of the Set interface. Its elements are stored in an order and cannot be duplicated. You can also map the elements to Java. util. sortset interface attributes. Its elements can be sorted by natural attributes.

(2)<List> element: The ing type can be Java. util. the attribute of the List interface. It needs to store the position of each element in the database table that combines the attribute object with an additional index column, that is, the attribute can be repeated.

(3)<Bag> element: Can map Java. util. collection interface attributes. Its elements may be repeated, but the attributes are not saved, which is similar to set. Because of its existence, if list is usually used, if you do not want to add a column, use it.

(4)<Map> element: Can be mapped to Java. util. the attributes of the map interface. Its elements are saved as key-value pairs, which are unordered. You can also map the elements to Java. util. sortmap interface attributes. Its elements can be sorted in natural order.

(5)<Array> element: The ing type can be an array attribute, but it is rarely used in practical use.

 

In this blog, we will mainly look at the details of map property ing.

The map attribute is also a common attribute type. For example, if there are multiple students in a group, the Student name corresponds to the corresponding name. Below we will take a detailed look at this example:

 

First, let's take a look at the entity: Team. Java

Public class team {private stringid; private stringteamname; private mapstudents = new hashmap (); ************* set, get method}

 

Next, let's take a look at the specific object ing configuration.

Team. HBM. xml

 

<? XML version = "1.0"?> <! Doctype hibernate-mapping public "-// hibernate/hibernate DTD ing DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

From the preceding configuration file, we can see that other configurations in the map property ing configuration file are the same as those in the previous configuration, but they are a little different when configuring the map property, next, let's take a look at the map attribute configuration. The first thing we need to explain is,Although we only have one configuration file, the above configuration will generate two database tables, and the map attribute will generate a separate database table, which contains the key and value of the map, there is also a foreign key.The key tag in the map attribute configuration specifies the ID of the team referenced by the data in the map table, that is, the foreign key corresponds to the primary key of the team. The index tag in the map tag corresponds to the key value in the map data, and the element tag specifies the value in the map data.

 

In fact, the value in the map we see above is only a simple data type, but in actual development, the value here is mostly in the form of objects. Now let's take a look at the above example. How should we configure the value here when it is an object: When the value is an object, we can no longer simply use a configuration file to generate two database tables. In this case, we need to create our team class. You also need to create the student object class corresponding to the value.

First, let's take a look at the entity: Team. Java

Public class team {private string ID; private stringteamname; private mapstudents = new hashmap (); ********* the set and get methods are omitted}

Student. Java

Public class student {private string ID; private stringcardid; private stringname; private int age; private team; ******* the set and get methods are omitted}

Next, let's continue to look at the object ing files.

 

Team. HBM. xml:

<? XML version = "1.0"?> <! Doctype hibernate-mapping public "-// hibernate/hibernate DTD ing DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

 

We can see from the above configuration file that this configuration file is almost the same as a simple data type configuration file, the only difference is that the element label in the map label is replaced by the one-to-one label, which fully describes:

 

The element sub-tag in the map tag maps the atomic type (string, date, Int, long ...), That is, the type that can be directly mapped to the database table field, while one-to-one ing is the entity type, which means that it cannot be mapped to a field in the table, it is the type mapped to the entire table.However, do not set inverse = true in One-to-minus of the map label, because if you want the objects in the MAP value to maintain the relationship between the two, it is very likely that the foreign key cannot be added in the stored data and is set to null.

Next let's take a look at student. HBM. xml.

<?xmlversion="1.0"?><!DOCTYPEhibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

Careful students can find that this configuration file is similar to the configuration of multiple parties in a multi-link ing, so I will not repeat it here.

 

 

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.