Hibernate efficient query: only partial/specified fields are queried.

Source: Internet
Author: User
Tags what sql

The company uses [java] DetachedCriteria detachedCriteria = DetachedCriteria. forClass (PeBulletin. class); detachedCriteria. createAlias ("enumConstByFlagIsvalid", "enumConstByFlagIsvalid"); detachedCriteria. createCriteria ("peSite", "peSite"); detachedCriteria. createCriteria ("peManager", "peManager"); detachedCriteria. add (Restrictions. eq ("enumConstByFlagIsvalid. code "," 1 "); detachedCriteria. createAlias ("enumConst ByFlagIstop "," enumConstByFlagIstop "); detachedCriteria. addOrder (Order. desc ("enumConstByFlagIstop. code ")). addOrder (Order. desc ("publishDate"); DetachedCriteria detachedCriteria = DetachedCriteria. forClass (PeBulletin. class); detachedCriteria. createAlias ("enumConstByFlagIsvalid", "enumConstByFlagIsvalid"); detachedCriteria. createCriteria ("peSite", "peSite"); detachedCriteria. createCriteria ("peManager ", "PeManager"); detachedCriteria. add (Restrictions. eq ("enumConstByFlagIsvalid. code "," 1 "); detachedCriteria. createAlias ("enumConstByFlagIstop", "enumConstByFlagIstop"); detachedCriteria. addOrder (Order. desc ("enumConstByFlagIstop. code ")). addOrder (Order. desc ("publishDate"); Queries data in QBC mode. The biggest advantage of this method is that it is almost completely object-oriented. It is an object encapsulated at the upper level of HQL, and almost forgets what SQL is. But he has the biggest drawback: efficiency. By default, all fields of this object, including their associated objects, are queried ]. As shown in the preceding query, at least 100 fields are found. Efficiency problems can be imagined. I really don't want to write SQL or HQL, which is too troublesome, so I thought of the best way to optimize it. QBC provides partial query of fields. Maybe the author of hibernate is right. If hibernate is inefficient, you can only say that you still don't know how to use it, although this guy doesn't understand SQL before writing hibernate. Its level-2 cache is good. Let's get down to the truth. Hibernate uses some/specified fields to query. There are three types of word formats: the first method is to use the advanced query DetachedCriteria. The generation of tokens is as follows: [java] String alias = "user _"; // DetachedCriteria dc = DetachedCriteria. forClass (User. class, alias); ProjectionList pList = Projections. projectionList (); pList. add (Projections. property (alias + ". "+" id "). as ("id"); pList. add (Projections. property (alias + ". "+" name "). as ("name"); pList. add (Projections. property (alias + ". "+" Age "). as ("age"); pList. add (Projections. property (alias + ". "+" sex "). as ("sex"); dc. setProjection (pList); dc. setResultTransformer (Transformers. aliasToBean (User. class); resultList = memberService. findByDetached (dc ). size (); String alias = "user _"; // the other table name DetachedCriteria dc = DetachedCriteria during query. forClass (User. class, alias); ProjectionList pList = Projections. projectionList (); pList. add (Projec Tions. property (alias + ". "+" id "). as ("id"); pList. add (Projections. property (alias + ". "+" name "). as ("name"); pList. add (Projections. property (alias + ". "+" age "). as ("age"); pList. add (Projections. property (alias + ". "+" sex "). as ("sex"); dc. setProjection (pList); dc. setResultTransformer (Transformers. aliasToBean (User. class); resultList = memberService. findByDetached (dc ). size (); the second method is to use the HQL statement new POJO () to implement The method is as follows: [java] package com. domain; public class Link {private String id; private String name; private String url; private Integer index; public Link () {}// because: string hql = "select new Link (id, name) from Link"; // you must have a constructor that accepts two parameters: public Link (String id, String name) {this. id = id; this. name = name;} public String getName () {return name;} public void setName (String name) {this. name = name;} publ Ic String getUrl () {return url;} public void setUrl (String url) {this. url = url ;}} package com. domain; public class Link {private String id; private String name; private String url; private Integer index; public Link () {}// because: string hql = "select new Link (id, name) from Link"; // you must have a constructor that accepts two parameters: public Link (String id, String name) {this. id = id; this. name = name;} public String getName () {return name;} pu Blic void setName (String name) {this. name = name;} public String getUrl () {return url;} public void setUrl (String url) {this. url = url;} queries through the HQL statement [java] String hql = "select new Link (id, name) from Link"; Query query = session. createQuery (hql); // The default queried list stores an Object, but the list stores no longer the default Object, instead, the Link object lists <Link> links = query. list (); for (Link link: links) {String id = link. getId (); Strin G name = link. getName (); System. out. println (id + ":" + name);} String hql = "select new Link (id, name) from Link"; Query query = session. createQuery (hql); // The default queried list stores an Object, but the list stores no longer the default Object, instead, the Link object lists <Link> links = query. list (); for (Link link: links) {String id = link. getId (); String name = link. getName (); System. out. println (id + ":" + name);} the third method is implemented through an HQL statement, similar to an SQL statement. The method is as follows :[ Java] String hql = "select id, name from Link"; Query query = session. createQuery (hql); // by default, the queried list stores an Object array and needs to be converted to the corresponding javaBean. List <Object []> links = query. list (); for (Object [] link: links) {String id = link [0]; String name = link [1]; System. out. println (id + ":" + name);} String hql = "select id, name from Link"; Query query = session. createQuery (hql); // by default, the queried list stores an Object array and needs to be converted to the corresponding javaBean. List <Object []> links = query. list (); for (Object [] link: links) {String id = link [0]; String name = link [1]; System. out. println (id + ":" + name );}

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.