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 code The Code is as follows: // return 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 codeCopy 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

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.