Select new map () for Hibernate Optimization ()

Source: Internet
Author: User

Hibernate tuning is not just about setting lazy, but about who maintains this field.

This time we want to optimize the query statement -- select new map ().

Description of the select new map statement.

Statement 1:

String hql = "select s. name from Student s ";

List ls = session. createQuery (hql). list ();

For (String obj []: ls ){

System. out. pringln (obj [0]);

}

In the result list, each record corresponds to an object array, and each element in object [] is the sequence number of the columns in the hql Statement (starting from 0 ).

Statement 2:

String hql = "select new map (s. name) from Student s ";

List ls = session. createQuery (hql). list ();

For (Map m: ls ){

System. out. pringln (m. get ("0 "));

}

In the result list, each record corresponds to a map. In the map, the key is the serial number in the hql statement, starting from 0, and the key is a character, not a number.

Statement 3:

String hql = "select new map (s. name as name) from Student s ";

List ls = session. createQuery (hql). list ();

For (Map m: ls ){

System. out. pringln (m. get ("name "));

}

In the result list, each record corresponds to a map, and the key in the map is the alias in the hql statement.

Detailed description of map

The map here corresponds to the HashMap in JDK.

In my opinion, Hibernate encounters the map keyword when parsing this hql statement, and then uses the following column as the value and alias as the key (if there is no alias, it should be replaced by a number) stored in a HashMap. The specific code does not look at, who is interested to find the code and send it.

Explain the efficiency of select new map

I first saw this code, and the first thought was that it reduced the efficiency. It involves field analysis. We also think that map is not the jdk map but the mapping map in hibernate.

In mapping, map only has one constructor, new Map (PersistentClass owne ). So I initially thought it was to encapsulate the field into a PersistentClass class, and then create a Map in new.

But later I found that this is not the class. The classes in mapping are used for ing files, that is, my direction is wrong.

But does the select new map statement reduce the efficiency or improve the efficiency?

To put it simply, when you write an SQL statement, is the efficiency of select * from high? Is the efficiency of select Field 1 and Field 2 from high?

Obviously, the former must not be more efficient than the latter, because the latter can only select useful data for transmission.

Now let's look back at select new map. Isn't that the truth? The following is an example of efficiency comparison (I use 100 pieces of data ).

Single Table query

// Start Query
Long startTime = System. currentTimeMillis ();
String hql = "select new map (u. name as name) from User u ";
List <Map> list;
List = session. createQuery (hql). list ();
// Query completed
Double time = (System. currentTimeMillis ()-startTime );
NumberFormat numberFormat = new DecimalFormat ("0.000 ");
System. out. println (numberFormat. format (time/1000 ));
// Read data cyclically
For (Iterator iter = list. iterator (); iter. hasNext ();){
Map map = (Map) iter. next ();
System. out. println (map. get ("name "));
}
// Read the data cyclically.
Time = (System. currentTimeMillis ()-startTime );
System. out. println (numberFormat. format (time/1000 ));

For more details, please continue to read the highlights on the next page:

Hibernate details: click here
Hibernate: click here

Hibernate Chinese manual PDF

  • 1
  • 2
  • Next Page

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.