Six ways to Hibernate queries

Source: Internet
Author: User
Tags cdata


Hibernate query Six ways are hql query, Object query criteria method, dynamic query Detachedcriteria, example query, SQL query, named query .

If you simply use Hibernate to query the database only need to understand one of them can accomplish the general functions you want to achieve, but to learn more knowledge on the six methods, they provide more choices. Of course, each method has its own applicable conditions and prerequisites.

1, HQL Query

HQL is Hibernate's own set of query languages, different from SQL syntax, with the advantages of cross-database. Example code:

[Java]View Plain Copy
  1. static void query (String name) {
  2. Session s=null;
  3. try{
  4. S=hibernateutil.getsession ();
  5. //from is followed by an object, not a table name
  6. String hql="from admin as admin where admin.aname=:name";   Using named parameters, it is recommended to use, easy to read.
  7. Query query=s.createquery (HQL);
  8. Query.setstring ("name", name);
  9. List<admin> list=query.list ();
  10. For (Admin admin:list) {
  11. System.out.println (Admin.getaname ());
  12. }
  13. }finally{
  14. if (s!=null)
  15. S.close ();
  16. }
  17. }

Application: Common method, more traditional, similar to JDBC. Cons: New query Language, limited applicability, applies only to hibernate framework.

2, the object of query criteria method :

[Java]View Plain Copy
  1. static void Cri (String name,string password) {
  2. Session s=null;
  3. try{
  4. S=hibernateutil.getsession ();
  5. Criteria C=s.createcriteria (Admin.   Class);
  6. C.add (Restrictions.eq ("Aname", name)); EQ is equal to, GT is greater than, LT is less than, or is or
  7. C.add (Restrictions.eq ("Apassword", password));
  8. List<admin> list=c.list ();
  9. For (Admin admin:list) {
  10. System.out.println (Admin.getaname ());
  11. }
  12. }finally{
  13. if (s!=null)
  14. S.close ();
  15. }
  16. }

Application: Object-oriented operation, innovation of the previous database operation mode, easy to read. Disadvantage: The application surface is more limited than HQL.

3, dynamic separation query Detachedcriteria

[Java]View Plain Copy
    1. Static List DC (Detachedcriteria DC) {
    2. Session s = hibernateutil.getsession ();
    3. Criteria C = Dc.getexecutablecriteria (s);
    4. List rs = c.list ();
    5. S.close ();
    6. return RS;
    7. }

[Java]View Plain Copy
  1. Detachedcriteria DC = Detachedcriteria.forclass (User.   Class);
  2. int id = 1;
  3. if (id! = 0)
  4. Dc.add (Restrictions.eq ("id", id));
  5. Date age = new Date ();
  6. if (age! = null)
  7. Dc.add (Restrictions.le ("Birthday", age));
  8. List users = DC (DC);
  9. SYSTEM.OUT.PRINTLN ("offline query returns results:" + users);

Application: Object-oriented operations, separation of business and the underlying, do not require the field attribute ingestion to the DAO implementation layer. Disadvantage: The application surface is more limited than HQL.

4. Example Query

[Java]View Plain Copy
    1. static list example (user user)  {  
    2.   session s = hibernateutil.getsession ();   
    3.   list<user> users = s.createcriteria (User.class). Add (  
    4.     example.create (user)). List ();   
    5.   //&NBSP;LIST<USER>&NBSP;&NBSP;
    6.   // users2=s.createcriteria (user.class). Add (Example.create ( User). IgnoreCase ())   
    7.   //&NBSP;. Createcriteria ("Child"). Add ((example.create (user))). List ();   
    8.   return users;  
    9. &NBSP;}&NBSP;&NBSP;

Where applicable: Object-oriented operation. Disadvantage: The applicable surface is more limited than HQL, not recommended.

5. SQL query

[Java]View Plain Copy
    1. static List SQL () {
    2. Session s = hibernateutil.getsession ();
    3. Query q = s.createsqlquery ("SELECT * from User"). Addentity (user.   Class);
    4. List<user> rs = q.list ();
    5. S.close ();
    6. return RS;
    7. }

Application: Not familiar with hql friends, and do not intend to transfer database platform friends, omnipotent method shortcomings: the destruction of cross-platform, difficult to maintain, not object-oriented.

6. Named query

[Java]View Plain Copy
    1. Static List namedquery (int id) {
    2. Session s = hibernateutil.getsession ();
    3. Query q = s.getnamedquery ("Getuserbyid");
    4. Q.setinteger ("id", id);
    5. return q.list ();
    6. }

[Java]View Plain Copy
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en"
  3. "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
  4. <class Name="Com.sy.vo.User" table= "User" catalog="News" >
  5. </class>
  6. <!--named queries: Defining query Conditions--
  7. <query name="Getuserbyid" >
  8. <! [Cdata[from User where id=:id]]>
  9. </query>
  10. < using SQL in!--named queries, deprecated, affecting cross-database
  11. <sql-query name="GetUserById2" >
  12. <! [Cdata[select * from User where]]>
  13. </sql-query>

Six ways to Hibernate queries

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.