Java knowledge 13 Hibernate Query Language (HQL), annotated version of this article an example to explain to hibernate

1, a brief overview          

1.1、

1)  SQL: oriented database * from tableName the SELECT;
2)  HQL query (Hibernate Query language): query language object-oriented hibernate offer.
    Example: session.createQuery ( "from tableName")  // and 1) point identity of the SQL statement, check out the effect is the same
3)  Criteria query , fully object-oriented query (Query By Criteria, QBC)
4 )  SQLQuery , native SQL query logic is more complex, HQL difficult to achieve

      Disadvantages: can not cross-database platform, if changed database, sql statement is likely to change
      usage scenarios: For complex sql, hql situation can not be achieved, you can use a local sql query

HQL queries to note:
      1, when hql query auto-import = "true" To set true, if it is false, write hql when you want to specify the full name of the class , for example:
           1.1, Query q = session.createQuery ( "from the Employee "); // = Auto-Import "to true" when
            1.2, Q = session.createQuery Query ( "from com.shore.model.Employee "); // = Auto-Import "to false" when

       2, with annotations version when, Auto-Import = "to true" (default to true) , write do not write the full name of the class, are possible. With xml configuration version , you need to specify the Auto-Import = "to true" , this province code is not specified is true, then write as little as 1.2 point 1 point above.

1.2, query all the columns

1 Query query = session.createQuery("from Employee"); //OK
2 Query query = session.createQuery("from com.shore.model.Employee"); //OK
3 Query query = session.createQuery("select * from Employee"); //错误,不支持 *
4 Query query = session.createQuery("select e from Employee e"); // OK
5 System.out.println(query.list()); //把结果打印到控台上

1.3, query specifies column

1 Query query = session.createQuery("select name,sex,salary from Employee");
2 System.out.println(query.list());

1.4, the query specified column, automatic packaging object

1 Query query = session.createQuery("select new com.shore.model.SubEmployee(name,sex,salary) from Employee");
2 System.out.println(query.list());

1.5 Conditions inquiry

  1.5.1, the query conditions placeholder (?)

1 Query query = session.createQuery("select new com.shore.model.SubEmployee(name,sex,salary) from Employee where department_id = ?");
2 //query.setParameter(0, 1);//或者下面的setInteger(0, 1)方法也行
3 query.setInteger(0, 1);
4 System.out.println(query.list());

  1.5.2, the conditions of inquiry named parameters ( : )

1 Query query = session.createQuery("select new com.shore.model.SubEmployee(name,sex,salary) from Employee where department_id = :deptID");
2 query.setParameter("deptID", 1);
3 System.out.println(query.list());

  1.5.3, the query conditions range queries (between .... and ...)

1 Query query = session.createQuery("select new com.shore.model.SubEmployee(name,sex,salary) from Employee where department_id between :d1 and :d2");
2 query.setParameter("d1", 1);   //也可以用占位符?实现
3 query.setParameter("d2", 2);
4 System.out.println(query.list());

  1.5.4, the conditions of fuzzy search query   like

1 Query query = session.createQuery("select new com.shore.model.SubEmployee(name,sex,salary)  from Employee where name like ?");
2 query.setParameter(0, "%三%");
3 System.out.println(query.list());

1.6, the statistical aggregate functions

1 Query query = session.createQuery("select count(*) from Employee where name like ?");
2 query.setParameter(0, "%三%");
3 System.out.println(query.list());

1.7, grouping query ( Group by )

1 Query query = session.createQuery("select department,count(*) from Employee group by department_id"); //根据部门外键
2 System.out.println(query.list());

1.8, join queries (the connector, the external connector, left / right connections)

  En: the Join the Join Inner ==  // intersection
  left join: Left join, to the left of the table the primary table, if not the join, the left side of the table data integrity, and data to the right of the table may be empty. All data // left of the table + table on the right part of the intersection of
  the right connection: Right join, the right of the main table to table, if not the join, the right table data integrity, data left of the table may be empty. + // intersection of all the data table on the right of the left portion of the table
  outer connection: Outer the Join  // sets and

1  connector 1) [mapping relation has been configured, the associated time, the properties of the object can be written directly]
 2 Query session.createQuery Q = ( "E from Employee Inner the Join e.department" ); // Employee Department is a foreign key (department_id Employee table) mapping entity
 . 3  
. 4  2) connected to the left
 . 5 Query session.createQuery Q = ( "E from the Employee left the Join e.department" );
 . 6  
. 7  . 3) right outer join
 . 8 Query session.createQuery = Q ( "Department D from right the Join d.employee" ); a foreign key (employee_id) // mapping
 . 9  
10  4) connected to the pressing FETCH Usage, data will right table, the table is filled to the left object! ]
 11 Query q = session.createQuery("from Employee e inner join fetch e.department");
12 q.list();
13         
14 5) 迫切左外连接
15 Query q = session.createQuery("from Employee e left join fetch e.department");

1.9, HQL query optimization

. 1 Query Query = session.getNamedQuery ( "getAllDept"); // define a parameter name getAllDept 
2 query.setParameter (0, 10);

  Then in the corresponding xml configuration file to write SQL statements. (For hair complicated SQL statements, you can do so, to facilitate the project after release, you can modify)

1  example, in: Employee.hbm.xml sql statement stored in xml 2      < Query name = "getAllDept" >   // parameter name here getAllDept to above correspond
 . 3          <[CDATA [! 
. 4              from the Employee D WHERE DEPARTMENT_ID <? ;   // here to write SQL statements, not statements HQL
 5          ]]> 
6      </ Query >

2, HQL query language example  

 

 

 

 

 

 

 

 

Original author: DSHORE

Home Author: http://www.cnblogs.com/dshore123/

From the original: https://www.cnblogs.com/dshore123/p/11546575.html

Welcome reproduced, reprinted, be sure to indicate the source. ( If this article helpful, you can click the bottom right corner of the recommendation , or comment, thank you! )

Guess you like

Origin www.cnblogs.com/dshore123/p/11588358.html