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 );
}