Set of Hibernate mappings

Source: Internet
Author: User
Tags set set

First, set mapping

1. Set interface

For example, a 1:n relationship, an object that corresponds to multiple objects, how are these objects saved? You need to save these objects using a collection. One category corresponds to a number of items.

  

2, the difference between the three

(1) HashSet

HashSet has the following characteristics

L cannot guarantee the order in which the elements are arranged, and the order may vary

The l is not synchronous

The L collection element can be null, but only one null

When an element is deposited into a hashset union, HashSet calls the object's Hashcode () method to get the Hashcode value of the object, and then determines where the object is stored in hashcode based on the HashSet value.

Simply put, the hashset set determines that two elements are equal by two objects that compare equality by equals method, and two objects with the Hashcode () method return values equal

Note that if you want to put an object in HashSet and override the Equals method of the corresponding class for that object, you should also override its Hashcode () method. The rule is that if two objects return true through the Equals method, their hashcode should also be the same. In addition, the attribute used in the object as the equals comparison criterion should be used to calculate the value of the hashcode.

(2) Linkedhashset

The Linkedhashset collection also determines where the element is stored, based on the hashcode value of the element, but it maintains the order of the elements using the linked list. This makes the elements appear to be saved in an insertion order, that is, when the collection is traversed, Linkedhashset will access the elements of the collection in the order in which they are added.

Linkedhashset performance is better than hashset when iterating through all the elements in the set, but the performance is slightly inferior to hashset at insert time.

(3) TreeSet class

TreeSet is the only implementation class for the SortedSet interface, and TreeSet ensures that the collection elements are in the sorted state. TreeSet supports two sorting methods, natural sorting and custom sorting, in which the natural sort is the default sorting method. The object that is added to the treeset should be the same class.

TreeSet determines that two objects are unequal by two objects returning false through the Equals method, or by comparing the CompareTo method without returning 0

L Natural Sorting

Natural sorting uses the CompareTo (Object obj) method to sort the elements to compare the size relationships between elements, and then arranges the elements in ascending order.

Java provides a comparable interface that defines a CompareTo (object obj) method that returns an integer value that implements the object of the interface to compare the size.

The Obj1.compareto (obj2) method returns 0, indicating that the two objects being compared are equal, and if a positive number is returned, the OBJ1 is greater than obj2, and if it is negative, obj1 is less than obj2.

If we always return true for the Equals method of two objects, then the CompareTo method returned by the two objects should return a 0

L Custom Sort

Natural sorting is based on the size of the collection elements, in ascending order, if you want to customize the sort, you should use the comparator interface to implement the int compare (T o1,t O2) method.

3. Example

  

(1) Initialize the database. (Build table)

(2) Build the Persistence class (the user Class) and add the attribute in the class emails the value of all the email columns used to hold the object. and add getter and setter methods.

Import Java.util.HashSet;

Import Java.util.Set;

public class User implements Java.io.Serializable {

Private static final long serialversionuid = 3855149643658782259L;

Private Integer ID;

Private String username;

private String password;

Use a collection to represent all of this person's email

Private Set emails = new HashSet ();

Public User () {

}

Setting the set and Get methods

Public Set getemails () {

return emails;

}

public void Setemails (Set emails) {

This.emails = emails;

}

Public Integer getId () {

return this.id;

}

public void SetId (Integer id) {

This.id = ID;

}

Public String GetUserName () {

return this.username;

}

public void Setusername (String username) {

This.username = Username;

}

Public String GetPassword () {

return This.password;

}

public void SetPassword (String password) {

This.password = password;

}

(3) Modify the User.hbm.xml file as follows:

  

  

"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

  

  

  

  

  

  

  

Description of the file:

  

Also, because the user table is not available in SQL Server, enclose it in brackets.

(4) test file, as follows:

Import Java.util.Set;

Import org.hibernate.Session;

Import org.hibernate.Transaction;

Import Com.pojo.User;

Importcom.utils.HibernateSessionFactoryUtil;

public class Gbtest {

public static void Main (string[] args) {

TODO auto-generated Method Stub

Get ();

}

public static void Get () {

Session session = Hibernatesessionfactoryutil.getsessionfactory (). Getcurrentsession ();

Transaction tx = Session.begintransaction ();

User U = (user) Session.load (user.class, New Integer (3));

System.out.println (U.getusername ());

System.out.println (U.getpassword ());

Set set = U.getemails ();

for (Object O:set) {

System.out.println (o);

}

This sentence must be disposed of, otherwise it will be an error

Tx.commit ();

}

public static void Add () {

Session session = Hibernatesessionfactoryutil.getsessionfactory (). Getcurrentsession ();

User U = new user ();

U.setusername ("Bjl");

U.setpassword ("8888");

U.getemails (). Add ("[email protected]");

U.getemails (). Add ("[email protected]");

Transaction tx = Session.begintransaction ();

Session.save (U);

Tx.commit ();

}

}

Second, List interface

  

1, the difference between the two

General differences between ArrayList and LinkedList:

L ArrayList is a data structure based on dynamic array, LinkedList data structure based on linked list.

L for random access get and set,arraylist feel better than LinkedList, because LinkedList want to move the pointer.

The Add and remove,linedlist for new and deleted operations are dominant because ArrayList is moving data.

2. Performance comparison

ArrayList and LinkedList have their own advantages and disadvantages in performance, and each has its own applicable place, which can be described as follows:

For ArrayList and LinkedList, the overhead of adding an element at the end of the list is fixed. For ArrayList, the main point is to add an entry in the internal array, pointing to the element being added, which may occasionally cause the array to be redistributed, whereas for LinkedList, the overhead is uniform, allocating an internal entry object.

l inserting or deleting an element in the middle of the ArrayList means that the remaining elements in the list will be moved, while the overhead of inserting or deleting an element in the middle of the linkedlist is fixed.

L LinkedList does not support efficient random element access.

L ArrayList space waste is mainly reflected in the list at the end of the table to reserve a certain amount of space, and the linkedlist of the space cost is reflected in its every element needs to consume considerable space

It can be said that using ArrayList provides better performance when the action is to add data after a column of data rather than in front or in the middle, and to randomly access its elements, and when your action is to add or delete data in front or in the middle of a column of data, and to access its elements sequentially, You should use the LinkedList.

3. Example (1:04)

Use the list above to implement the example.

(1) Modify the structure of the table (because the list is to record the order of insertions), increase the IDX field (integer)

(2) Modify the emails type in the above User.java to list. The code is as follows:

Private List emails = new ArrayList ();

Public User () {

}

Setting the set and Get methods

Public List getemails () {

return emails;

}

public void Setemails (List emails) {

This.emails = emails;

}

(3) Modify the User.hbm.xml mapping file with the following code:

  

  

  

  

  

(4) Modify the test class to modify all set types to the list type

(5) After the program runs, the results in the database are as follows:

  

PS: In the future application, it is often used set,list is not commonly used, because the record sequence is of little significance.

Set of Hibernate mappings

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.