QBC retrieval and local SQL retrieval

Source: Internet
Author: User
Tags rowcount

QBC:
QBC Query by Criteria: This method compares object -oriented, because it is object-oriented, so the query parameter name is the property name of the queried class is not the column name of the database table
The point is that there are three objects that describe the condition: restrictions,order,projections.
The following three steps are generally required to use QBC queries:
1. Use the Createcriteria () method of the session instance to create the criteria object,
2. And using the Add () method to set the query condition, the tool class restrictions the criteria object set the query criteria, the Order tool class methods to set the sorting method, projections tool class methods for statistics and grouping.
3. Use the list () method of the criteria object to query and return results

code example:

@Test Public voidTestqbcquery () { criteria=session.createcriteria (Employee.class);//1 creates a criteria Criteria.add (RESTRICTIONS.GT ("Salary", 4000F)) 2 Add a query condition by using Add (). AddOrder (Order.desc ("Salary"). Setprojection (Projections.count ("Dept"), or//This is the dept_id field in the database table, so write the Dept property directly in the class, and you cannot write the table column name down
After setting the projections object to return only the condition of the projections setting, do not return the employee object to me,? List<Employee> result=criteria.list ();//Use the list () method to execute the query statement and return the result for(Employee emps:result) {System.out.print (Emps.getid ()+" "); System.out.println (Emps.getname ()+" "+emps.getsalary ()); } }

QBC retrieving the three most important classes

 Common methods of the restrictions class: The return type is criterion, and the parameter method name as a query container is described using the Restrictions.eq equals Restrictions.eq (String propertyname,object value) Restrictions.alleq uses map,                        Key/valu multiple equals to Restrictions.alleq (Map propertynamevalues) restrictions.gt (greater than) greater than                    RESTRICTIONS.GT (String PropertyName, Object value) restrictions.ge (greater equal) greater than or equal to Restrictions.ge (String PropertyName, Object value) restrictions.lt (less than) smaller than restrictions . It (String propertyname, Object value) restrictions.le (less equal) is equal to Restrictions.le (String Pro Pertyname, Object value) restrictions.between corresponds to SQL between Restrictions.between (String property Name, Object lo, Object hi) restrictions.like the like restrictions.like corresponding to SQL (String PropertyName, Object VALue) restrictions.in corresponding to SQL in restrictions.in (String PropertyName, Collection value) Re           Strictions.and and Relationship Restrictions.and (Criterion LHS, Criterion RHS) restrictions.or or relationship restrictions.or (Criterion LHS, Criterion RHS) restrictions.sqlrestriction SQL Limited Search Inquiry Restrictions.sqlrestriction (String sql,object[] values,type[] types)
  
Common methods of the projections class: Parameter method name descriptions as query  containers    use Projections.avg to   average   porject Ions.avg (String propertyname)  Projections.count counts the number of a property  Projections.count (String propertyname)  Projections.countdistinct count the number of different values for a property        projections.countdistinct (String PropertyName) Projections.groupproperty  Specifies that a property is a grouping property   Projections.groupproperty (String PropertyName) Projections.max  Maximum value   Projections.max (String PropertyName)  projections.min   To     find the minimum value  Projections.min (String PropertyName)  projections.projectionlist Create a Projectionlist object Projections.projectionlist ()  projections.rowcount  number of record bars in query result set         Projections.rowcount ()  Projections.sum   Total  projections.sum (String PropertyName) for a property                                                

Local SQL Retrieval: is database-oriented, so the retrieved property must be a database table name or a table field, not an object class name or property name.

However, queries for local SQL statements are not recommended in Hibernate, because queries from local SQL statements can make the program unstable and not be able to migrate the database freely. Because each database has a unified SQL standard, but each data has a lot of its own expansion, resulting in incompatible SQL statements, resulting in the instability of the program function.
Local SQL Retrieval Example:
1 getting the Query object Session.createsqlquery (SQL)
2 Add a query condition. setString ("Tablefiledname", "value")
3 executes the query statement and returns the result. Executeupdate ();
code example:

@Test      Public void Testnativesql () {        = "INSERT into gg_department VALUES (?,?)" ;         = session.createsqlquery (sql);                Query.setinteger (0, 280)             . setString (1, "Atguigu")             . executeupdate ();    

QBC retrieval and local SQL retrieval

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.