Core interface query interface usage in Hibernate

Source: Internet
Author: User

Core interface query interface usage in Hibernate

Query is a Hibernate Query interface used to Query objects from data storage sources and control the Query execution process. Query encapsulates an HQL Query statement.

Query Interface Usage:

After obtaining the session object through SessionFactory, you can get the corresponding object through the get (class name. class, id) method, and get the required object through the Query object.

 

Query query = session. createQuery (the Query statement is like from UserBean );

The Query object is valid before the Session object is closed. Otherwise, a SessionException is thrown. The Session object is the Connection object in JDBC, that is, a Connection to the database. When the Connection object is closed, the Statement object cannot be used any more. Therefore, after the Session is closed, the Query object cannot be used any more.

 

Query interface method:

Common Query interface methods:

1. setxxx (): used to set the question mark or variable value in an HQL statement;

You can set the question mark or variable value in an HQL statement in either of the following ways:

A, setString (int position, String value); Set "?" In HQL Value, where position represents "?" In HQL, the value must be "?" Set Value

Example:

 

Query query = session. createQuery (from UserInfoPO u where u. age>? And u. useName like ?); Query. setInteger (0, 22); // use ?, The first subscript starts from 0. Assign 22 query. setString (1, % %) to the first question mark; // set the value of the second question mark to "% Zhi %"

 

 

B, setString (String paraName, String value); set the value of the variable after ":" In HQL. paraName indicates the variable name after ":" In HQL, value is the value of the variable:

Example:

 

Query query = session. createQuery (from UserInfoPO u where u. age>: minAge and u. useName like: useName); query. setInteger (minAge, 22); // set the minAge value query. setString (userName, % %); // set the useName Value

 

2. list (); returns the query result and converts the query result to a list object;

You can also use query. uniqueResult (); // to obtain a single object.

 

Query query = session. createQuery (from UserInfoPO u where u. age>: minAge and u. useName like: useName); query. setInteger (minAge, 22); // set the minAge value query. setString (userName, % %); // set the useName Value List
 
  
List = query. list (); for (int I = 0; I
  
   

 

3. executeUpdate (); execute the update and delete statements.

 

Query query = session. createQuery (delete from UserInfoPO); query.exe cuteUpdate (); // delete data

Paging query:
Query. setFirstResult (The position is 0); // indicates the position from which the query starts and the query object is returned.
Query. setMaxResult (number of records); // indicates the number of records on the page. A set is returned.
Session. createQuery (select count (*) from class name). uniqueResult (); // get the total number of records

 

 

Describes Query statements.

Use HQL to delete data:

 

   String hql = delete user where age>18;    Query query = session.createQuery(hql);    int ref = query.executeUpdate();  
Update Data:

 

 

   Transaction tx= session.beginTransaction();    Query query = session.createQuery(update User set name='momor' where name='bbb');    query.executeUpdate();    tx.commit();    session.close(); 
You can also perform expressions on the where clause, and, or:
Query query = session.createQuery(from User user where (user.age / 10 = 3)); 
Query query = session.createQuery(from User user where (user.age > 20) and (user.name = 'caterpillar')); 
Is not nullL and is null can test whether the field value is null:

 

 

Query query = session.createQuery(from User user where user.name is not null); 
Between can test whether the field value is within the specified range:

 

Query query = session. createQuery (from User user where User. age between 20 and 30 );

You can use in or not in to test whether the field value is in the set you specified:

 

Query query = session.createQuery(from User user where user.name in('caterpillar', 'momor'));
Like or not like allows you to perform fuzzy search. For example, you want to search for data whose names start with cater:

 

 

Query query = session.createQuery(from User user where user.name like 'cater%'); 
Use order by to sort the query results. Use desc to reverse sort the results:

 

 

Query query = session.createQuery(from User user order by user.age); 
Query query = session.createQuery(from User user order by user.age desc, user.name); 

Code application:

 

 

Public static void query (String name) {Session session = null; try {session = HibernateSessionFactory. getSession (); // defines the hql statement to query all String queryString = from User as user where user by name. name =: n; Query query = session. createQuery (queryString); query. setString (n, name); // query all lists with the same name
    
     
List = query. list (); // obtain all result sets for (User u: list) {System. out. println (u. toString () ;}} finally {if (session! = Null) {session. close ();}}}
    


 





 

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.