Hibernate query method summary

Source: Internet
Author: User

1. get () and load ()
Java code
Session. get (Clazz, id );
Session. load (Clazz, id );
Session. get (Clazz, id );
Session. load (Clazz, id );
Note: differences between load () and get ()
Note that if no matching database records exist, the load () method may throw an unrecoverable exception ). If a proxy is used for class ing, the load () method returns an uninitialized proxy until you call a method of the proxy to access the database. If you want to create an association pointing to another object in an object, and you do not want to load the associated object at the same time when loading the object from the database, this operation method can be used. If batch-size is set for the corresponding class ing relationship, this operation allows multiple objects to be loaded in batches (because the proxy is returned, you do not need to capture the data of all objects from the database ).
If you are not sure whether a matched row exists, you should use the get () method, which will immediately access the database. If no corresponding row exists, null is returned.
2. HQL
Java code
  
Copy codeThe Code is as follows:
// Returns a row of records
String hql = "from TOrder o where o. id =? ";
TOrder o = (TOrder) s. createQuery (hql)
. SetParameter (0, orderId)
. UniqueResult ();
// Name Parameters
Query q = sess. createQuery ("from DomesticCat cat where cat. name =: name ");
Q. setString ("name", "Fritz ");
// Location Parameter
Query q = sess. createQuery ("from DomesticCat cat where cat. name =? ");
Q. setString (0, "Izi ");
// Name parameter list
Query q = sess. createQuery ("from DomesticCat where cat. name in (: namesList )");
Q. setParameterList ("namesList", names );
// Query by PAGE
Query q = sess. createQuery ("from DomesticCat ");
Q. setFirstResult (20 );
Q. setMaxResults (10 );
List cats = q. list ();
3. Criteria
List cats = sess. createCriteria (Cat. class)
. Add (Restrictions. like ("name", "Fritz % "))
. Add (Restrictions. or (
Restrictions. eq ("age", new Integer (0 )),
Restrictions. isNull ("age ")
))
. AddOrder (Order. asc ("name "))
. AddOrder (Order. desc ("age "))
. List ();
  
// Returns a row of records
String hql = "from TOrder o where o. id =? ";
TOrder o = (TOrder) s. createQuery (hql)
. SetParameter (0, orderId)
. UniqueResult ();
// Name Parameters
Query q = sess. createQuery ("from DomesticCat cat where cat. name =: name ");
Q. setString ("name", "Fritz ");
// Location Parameter
Query q = sess. createQuery ("from DomesticCat cat where cat. name =? ");
Q. setString (0, "Izi ");
// Name parameter list
Query q = sess. createQuery ("from DomesticCat where cat. name in (: namesList )");
Q. setParameterList ("namesList", names );
// Query by PAGE
Query q = sess. createQuery ("from DomesticCat ");
Q. setFirstResult (20 );
Q. setMaxResults (10 );
List cats = q. list ();

3. Criteria
  Copy codeThe Code is as follows:
List cats = sess. createCriteria (Cat. class)
. Add (Restrictions. like ("name", "Fritz % "))
. Add (Restrictions. or (
Restrictions. eq ("age", new Integer (0 )),
Restrictions. isNull ("age ")
))
. AddOrder (Order. asc ("name "))
. AddOrder (Order. desc ("age "))
. List ();

4. Native SQL
Java code
Copy codeThe Code is as follows:
String treeSql = "" +
"Select, level from tree t" +
"Start with t. parent_id = 0" +
"Connect by prior t. id = t. parent_id ";
List result = session. createSQLQuery (treeSql)
. AddEntity ("t", Tree. class)
. AddScalar ("level", Hibernate. INTEGER)
. List ();
String treeSql = "" +
"Select, level from tree t" +
"Start with t. parent_id = 0" +
"Connect by prior t. id = t. parent_id ";
List result = session. createSQLQuery (treeSql)
. AddEntity ("t", Tree. class)
. AddScalar ("level", Hibernate. INTEGER)
. List ();

5. Named SQL queries (not recommended)
6. filter (not recommended)
7. Detached queries (not tested yet)
The DetachedCriteria class lets you create a query outside the scope of a session, and then later execute it using some arbitrary Session

Related Article

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.