Hibernate-query partial Fields

Source: Internet
Author: User

The first method is to use the advanced query DetachedCriteria. The generation is as follows:

[Java] www.2cto.com
String alias = "user _"; // name of the table at the time of query
 
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 ();

Query through HQL statements
[Java]
String hql = "select new Link (id, name) from Link ";
 
Query query = session. createQuery (hql );
 
// By default, the queried list stores an Object, but the list stores no longer the default Object, but the Link Object.
 
List <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 );
 
}

 

 

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.