Compatible with the SQL statement Criteria of each database

Source: Internet
Author: User

Session session = HibernateUtil. getSessionFactory (). openSession ();
Criteria criteria = session. createCriteria (People. class );

// List list = criteria. list (); // select * from people

// Criteria. add (Restrictions. gt ("id", 2); // select * from people where id> 2;

// Criteria. add (Restrictions. between ("id", 1, 5); // select * from people where id between 1 and 5;

// Criteria. add (Restrictions. isNull ("password"); // select * from people where password is noll;

// Criteria. add (Restrictions. eq ("name", "cf"); // select * from people where name = 'cf ';

// Criteria. add (Restrictions. like ("name", "_ f"); // select * from people where name like '_ F ';

// Criteria. add (Restrictions. lt ("id", 6); // select * from people where id <6;

// Criteria. add (Restrictions. and (Restrictions. ge ("id", 2), Restrictions. le ("id", 3 )));
// Select * from people where id> = 2 and id <= 3;

// Criteria. add (Restrictions. or (Restrictions. eq ("name", "cf"), Restrictions. gt ("id", 2 )));
// Select * from people where id> 2 or name = 'cf ';

// Criteria. add (Restrictions. in ("id", new Integer [] {1, 2, 3 }));
// Select * from people where ID in (1, 2, 3 );

// Criteria. addorder (order. ASC ("name "));
// Select * from people order by name ASC;

// Criteria. setfirstresult (1 );
// Criteria. setmaxresults (2 );
// Select * from people limit 1, 2;

// Criteria. setprojection (projections. Count ("ID "));
// Select count (ID) from people;

// Criteria. setprojection (projections. AVG ("ID "));
// Select AVG (ID) from people;

// Criteria. setprojection (projections. groupproperty ("name "));
// Select name from people group by name;


// Criteria. setprojection (projections. rowcount ());
// Select count (*) from people;

// Projectionlist = Projections. projectionlist ();
// Projectionlist. Add (projections. groupproperty ("name "));
// Projectionlist. Add (projections. rowcount ());
// Criteria. setprojection (projectionlist );
// Select name, count (*) from people group by name;

List list = criteria. List ();

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.