Hibernate collection Mappings (set, List, Array, map, Bag)

Source: Internet
Author: User
Tags set set

POJOs as follows:

Customer class------>customer Table Order class corresponding---------->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<order> orders = new hashset<order> ();    /*setter and Getter method*/}

  

public class order{    private String ID;    Private String ordernumber;    private int balance;    Private customer customer;    /*setter and Getter method*/}

  

Set Set mapping:

Hibernate provides a dedicated tag element for a set map, which is represented by a set of <set> tags:

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd "> 

The "name" property in the

<set> tag represents the property name of the relationship collection in the Customer object, and the "inverse" and "cascade" property descriptions (see here). The "one-to-many" relationship in the database is implemented through a foreign key association, and the "multiparty" determines the relationship by holding the primary key value of the "one", and how to hold the primary key value of the "Party"? Multiple parties will use a column to store the primary key value of the "one", and then make the column the primary key column of the "Party" as the foreign key column. So when using hibernate to develop a relational column (foreign key column) for two tables, tell Hibernate,<key column= "customer_id" ></key> This is the job, hibernate can be based on the " customer_id "column to remove the associated information. 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 a record of equal value, and then assembles it into the Set collection property in the Customer object. Vice versa. Because the extracted records (just some bits and pieces of value that are not assembled into objects) need to be stored in the set set, tell hibernate what type of data it can put in the set set. <one-to-many> This tag is the way to do this, and the "class" attribute is the type that specifies the element within this set set.

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd "> 

  

<many-to-one> tags are "multiple" in the "one-to-many" relationship, name specifies which attribute is the relationship property, and "class" specifies the type of the Relationship property (also specifies which table is associated with it), and the "column" property specifies that the associated property is specified by the The value of the "customer_id" column, which is obtained in the Customer table.

Test:

tx = Session.begintransaction ();            /* Create a Customer object and set its property 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 property value */order Order1 = New Order ();            Order1.setordernumber ("a1a2a3");            Order1.setbalance (1000);                        Order1.setcustomer (customer);            /* * Create order Object Order2 and set its property value */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 (); 

  

To view data for a database:

Customer table:

Orders table

You can see that the data was successfully inserted into the database, and that the "custom_id" column (off series) was also correctly assigned.

List mappings:

Hibernate provides a dedicated tag element for the collection map, which is represented by the list collection map, using the <list> tag:

<list name= "Orders" inverse= "false" cascade= "All" >                <key column= "customer_id" ></key>                < Index column= "customer_index" type= "integer" ></index>                <one-to-many class= " Com.suxiaolei.hibernate.pojos.Order "/>            </list>

  

The list collection is sequential, the "index" label, which is used for the order of records, and the order of the list will appear on the "Customer_index" column, with the rest set similar to the set set. Note: The value in "inverse" in the list map cannot be set to "true" because the order of the list collection is only known to the customer, and the order party does not know the list exists. Otherwise, the column value of "Customer_index" will not be assigned.

To view the database:

Customer table:

Orders table:

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

Array (array) mapping: Label use <array> other is basically consistent with list.

Map map:

Hibernate provides a dedicated tag element for set mappings, map collection mappings, which are represented using the <map> tag:

<map name= "Orders" inverse= "false" cascade= "All" >                 <key column= "customer_id" ></key>                 < Index column= "Order_key" type= "string" ></index>                 <one-to-many class= " Com.suxiaolei.hibernate.pojos.Order "/>             </map>

  

The <index> tag in the map map, which represents the key value in the Map collection, is recorded in the "Order_key" column,<one-to-many> represents the Vlaue in the Map collection. The other settings are the same as above. Note : "Inverse" is not set to "true" because the key value is maintained by the customer object, and the order does not know the existence of the key.


Bag mapping: It is a combination of a list and a set set, which can be repeated but not smooth. Use list to simulate bag. Set-like, it also has a dedicated label <bag>.

Summarize

In the absence of special requirements, it is best to use the set set, because the set set does not have special information required by the "one side" maintenance, can be completely to "multi-party" maintenance, to improve performance, if you need to record the order of data can use the list and array mapping, if you need key/value form to store data, Map mappings can be used. Finally, if the data simple type (native type, wrapper class, String, date, and so on) of the set is slightly different on the collection map configuration, the,<element> element can directly map these simple types, and the other configurations are not the same as the above configuration.

Hibernate collection Mappings (set, List, Array, map, Bag)

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.