Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the set mapping of Hibernate

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about what the set mapping of Hibernate is. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

POJOs is as follows:

Customer class-> customer table

Order class corresponds to-> orders table

Customer (1) (n) order

Public class Customer {private String id; private String username; private String password; private Timestamp registerTime; private int age; private Set orders = new HashSet (); / * setter and getter method*/} public class Order {private String id; private String orderNumber; private int balance; private Customer customer; / * setter and getter method*/}

Set collection mapping:

Hibernate provides dedicated tag elements for collection mapping. Set collection mapping is represented by tags:

The "name" attribute in the tag represents the property name of the relationship collection in the customer object, and the "inverse" and "cascade" attribute descriptions (see here). The "one-to-many" relationship in the database is realized by foreign key association. "multi-party" determines the relationship by holding the primary key value of "one party". How to hold the primary key value of "one party"? "Multi-party" will use a column to store the primary key value of "one party", and then use this column as the primary key column of the foreign key column reference "party". So when using Hibernate development, you need to tell Hibernate the related columns (foreign key columns) of the two tables, that is, to complete this work, and Hibernate will be able to extract the associated information according to the "customer_id" column. For example, after fetching a record from the customer table, Hibernate looks for the "custom_id" column from the order table based on the primary key value of the customer record, takes the record with equal value, and assembles it into the set collection property in the Customer object, and vice versa. Because the extracted records (just bits and pieces of values that have not yet been assembled into objects) need to be stored in the Set collection, tell Hibernate what type of data can be put in the Set collection. This tag does this, and the "class" attribute specifies the type of element in the Set collection.

The tag sets the "multi-party" in an one-to-many relationship, name specifies which attribute is the relationship attribute, "class" specifies the type of the relationship attribute (and which table is associated with it), and the "column" attribute specifies that the associated attribute is queried in the customer table according to the value of the "customer_id" column.

Test:

Tx = session.beginTransaction (); / * * create a Customer object and set its attribute value * / Customer customer = new Customer (); customer.setUsername ("zhangsan"); customer.setPassword ("123456"); customer.setAge (22); customer.setRegisterTime (new Timestamp (new Date (). GetTime ()); / * create Order object order1 and set its attribute value * / Order order1 = new Order (); order1.setOrderNumber ("a1a2a3") Order1.setBalance (1000); order1.setCustomer (customer); / * * create an Order object order2 and set its attribute values * / Order order2 = new Order (); order2.setOrderNumber ("d3d2d1"); order2.setBalance (670); order2.setCustomer (customer); customer.getOrders (). Add (order1); customer.getOrders (). Add (order2); session.saveOrUpdate (customer); tx.commit ()

View the data in the database:

Customer table:

Orders table

You can see that the data is successfully inserted into the database, and the "custom_id" column (relational column) is assigned correctly.

List mapping:

Hibernate provides dedicated tag elements for collection mapping. List collection mapping is represented by tags:

The List collection is ordered, the "index" tag is used to record the order, the order of the List will be displayed on the "customer_index" column, and the rest of the settings are similar to the Set collection. Note: the value in "inverse" in the List map cannot be set to "true", because the order of the List collection is known only to the customer side, and the order side is not aware of the existence of the List. Otherwise, the column value of "customer_index" will not be assigned.

View the database:

Customer table:

Orders table:

You can see that the records are correctly inserted into the database, and "custom_index" correctly represents the order of the List.

Array (array) mapping: the tag uses something else that is basically the same as List.

Map mapping:

Hibernate provides dedicated tag elements for collection mapping. Map collection mapping is represented by tags:

The label in the Map map, which represents the key value in the Map collection, is recorded in the "order_key" column and represents the vlaue in the Map collection. The other settings are the same as above. Note: "inverse" should not be set to "true" because the key value is maintained by the customer object, and order is not aware of the existence of key.

Bag mapping: it is a combination of List and Set collections that can be repeated but not smooth. Use List to simulate Bag. Set up similar to Set, it also has a dedicated tag.

Total knot

Without special requirements, * use Set collections, because Set collections have no special information to be maintained by "one party" and can be fully maintained by "multiple parties", which can improve performance. If you need to record the order of data, you can use List and Array mapping, if you need to store data in key/value form, you can use Map mapping. * if the simple types of data placed in the collection (native types, wrapper classes of native types, String, Date, etc.) are slightly different in the configuration of collection mapping, elements can directly map these simple types, and other configurations are no different from the above configuration.

The above is what the collection mapping of Hibernate shared by Xiaobian is. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report