Hibernate--query Query __hql

Source: Internet
Author: User
Tags stub
The first two articles introduced the acquisition of Sessionfactory,session, as well as the session of the three States and the transition between, this article is bound to be the most concerned about the issue to be introduced, That is, you are most concerned about the database through the hibernate provided by the classes and methods of a deeper query.
This article is mainly about query queries

1. Introduce the entity class first

public class User {  
    //id field, primary key  
    private int id;  
    User name  
    private String username;  
    Password  
    private String userpass;  
    Parameterless construction method Public  
    User () {}  
    ///have parameter constructor public  
    user (int id,string username,string userpass) {  
        this.id= ID;  
        This.username=username;  
        This.userpass=userpass;  
    }  
    public int getId () {return  
        ID;  
    }  
    Appropriate getter setter public  
    void setId (int id) {  
        this.id = ID;  
    }  
    Public String GetUserName () {return  
        username;  
    }  
    public void Setusername (String username) {  
        this.username = username;  
    }  
    Public String Getuserpass () {return  
        userpass;  
    }  
    public void Setuserpass (String userpass) {  
        this.userpass = userpass;  
    }  
}  

2. After the introduction of entity classes, the data in the database to query 1 to get all the fields of the entire object

public static void Main (string[] args) {  
  //TODO auto-generated A stub session  
  Session=null;  
  Transaction Tx=null;  
  Session=hibernatesessionfactory.getsession ();  
  Tx=session.begintransaction ();  
  Queries all fields of the entire object  
  String hql= "from User";  
  Query query=session.createquery (HQL);  
  List<user> list=query.list ();  
  Iterator Its=list.iterator ();  
  while (Its.hasnext ()) {  
   user user= (user) Its.next ();  
   System.out.println (User.getid () + "" +user.getusername () + "" +user.getuserpass ());  
  }  
Console
1 Jordan 123
2 Rodman 123

2) Get a single property

public static void Main (string[] args) {  
        //TODO auto-generated A stub session  
        Session=null;  
        Transaction Tx=null;  
        Session=hibernatesessionfactory.getsession ();  
        Tx=session.begintransaction ();  
        Queries all fields of the entire object  
        String hql= "SELECT id from User";  
        Query query=session.createquery (HQL);  
        List<java.lang.integer> list=query.list ();  
        Iterator Its=list.iterator ();  
        while (Its.hasnext ()) {  
            int a= (Java.lang.Integer) Its.next ();  
            System.out.println (a);  
        }  
Console:
1
2

3 Query multiple properties when querying multiple properties, the returned list collection element type is a object[] array (that is, an array of objects), where the collection element is an array of objects, not the collection as an array of objects.

public static void Main (string[] args) {  
        //TODO auto-generated A stub session  
        Session=null;  
        Transaction Tx=null;  
        Session=hibernatesessionfactory.getsession ();  
        Tx=session.begintransaction ();  
        Queries all fields of the entire object  
        String hql= "Select Username,userpass from User";  
        Query query=session.createquery (HQL);  
        List<object[]> list=query.list ();  
Iterator Traversal        iterator its=list.iterator ();  
        while (Its.hasnext ()) {  
            object[] a= (object[)) Its.next ();  
            System.out.println (a[0].tostring () + "" +a[1].tostring ());  
        }  
    }  

4) Condition Inquiry

String hql= "from User where username=?" and userpass=? ";  
        Query query=session.createquery (HQL);  
        The first way of  
        //query.setstring (0, "Jordan");  
        Query.setstring (1, "123");  
        The second way of  
        query.setparameter (0, "Jordan", hibernate.string);  
        Query.setparameter (1, "123", hibernate.string);  
        List<user> list=query.list ();  
        for (User user:list) {  
            System.out.println (User.getid ());  
        }  

Query user name: Jordan, the password is 123 members.
Two ways, one is the setstring way, the other is the Setparameter way.
Output through enhanced for loop
Console:
1
And of course, the conditional query method of the custom index

public static void Main (string[] args) {  
        //TODO auto-generated A stub session  
        Session=null;  
        Transaction Tx=null;  
        Session=hibernatesessionfactory.getsession ();  
        Tx=session.begintransaction ();  
        Queries all fields of the entire object  
        String hql1= "from User where Username=:username and Userpass=:userpass";  
        Query query1=session.createquery (HQL1);  
        The first way of  
        /*query.setstring ("username", "Jordan"); 
         * Query.setstring ("Userpass", "123") 
         * *  
         *//The second general  

        , the third parameter determines the type  
        query1.setparameter ("username", "Jordan", hibernate.string);  
        Query1.setparameter ("Userpass", "123", hibernate.string);  
        List<user> list1=query1.list ();  
        for (User user:list1) {  
            System.out.println (User.getid ());  
        }  
    }  
Console:
1

Query way There are many, mostly in this extension of the above, compared to the criteria query, query way more flexible, more efficient.

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.