I. Concept of nested subqueries:In SQL, a select-from-where statement becomes a query block. Nesting a query block in the WHERE clause of another query BlockIn a condition of a having phrase, such a query is called a nested query or a subquery. For example:From student sWhere S. SnO IN(Select SnO from SC where CNO = '1 ')The preceding hql statement generated in the hibernate background is:SelectStudent0 _. ID as id1 _,Student0 _. Sno as sno1 _,Student
The in condition is special. Besides using the setparameterlist method, the placeholder must be enclosed by "()". Otherwise, an error is returned.
String hql ="Select P. querytemplate. ID, count (P. ID) from"+ Templateparameter. Class. getname ()+ "P where P. querytemplate. ID in (: idlist) group by P. querytemplate ";List result = Dao. Query (hql, "idlist", "1, 2, 3, 4, 6, 10, 11, 12,
Let the Framework do something to make SQL/hql/jdoql easier to write (on)
This article discusses how to write the SQL processing layer that exists between common SQL/hql statements and JDBC/hibernate and contains hundreds of lines of code.
In the open-source project, ibatis, an SQL-based ORM solution, can be referenced. In addition, it also has its new competitor orbroker, which is awesome and difficult to
When you write a HQL statement using Hibernate, I believe that there will be used to not equal to this condition, of course, most of them are used in the judgement is not equivalent to the case of empty strings, as for the HQL statement, in fact, and the general SQL statement does not make any difference, you can use!=, you can use the
We all know that it can be used in HQL. OR: * * * the way to configure query parameters externally, such as:
Query query=session.createquery ("from Teststu s where S.team=:team and S.age>:age"); Or
Query query=session.createquery ("from Teststu s where s.team=?") and S.age>?);
From a persistent team object, take out the collection of Teststu it holds and filter out the records of age greater than the execution data
We can set parameters like thisQ
We all know that you can use it in HQL. OR: * * * the way to configure query parameters externally, such as:
Query query=session.createquery ("from Teststu s where S.team=:team and S.age>:age"); Or
Query query=session.createquery ("from Teststu s where s.team=?") and S.age>?);
The meaning of this sentence is to remove the Teststu collection from a persistent team object, and to filter out records of age greater than the execution data
We can set t
Here is a simple HQL statement, because a lot of more complex outside query, with the general query difficult to completeSo we need to use HQL here.@Testpublic void Selquery () {System.out.printf ("Hello"); Configuration config = new configuration (); Sessionfactory sessionfactory = config.configure ("Hibernate.cfg.xml"). Buildsessionfactory (); Session session = Sessionfactory.opensession (); Transaction
When using the HQL query in today's project, the Qingaocenterinfo is not mapped[from Qingaocenterinfo where ...] appears.There is obviously a problem with the hibernate mapping relationship.This exception occurs first to see whether the table name in the database table is used in the query statement, not the entity class.To view my code:Centerlist = Manager.find ("From Qingaocenterinfo center where center.type =?") and Center.centername =? ", new Obje
In hibernate, the object class is queried using hql statements, and the returned result of the list method is a List. The objects encapsulated in the List are divided into the following three situations:1. When you query all fields, such as "from object class", the objects encapsulated in the list are the object class, And all attributes are filled.2. query only one field. By default, the Object is encapsulated in the list.3. query two or more fields.
When writing HQL, subqueries can be used except in the where condition. subquery statements are not allowed in other places, such as select and from. If subqueries are to be written after select, you can use the formula attribute to write data. If you want to use the subquery after the from statement, you cannot use HQL. Instead, use pure SQL (
Example of formula usage:
*. Hbm. xml Code
"1.0"?>
"-//
In HQL, the requirements for the count () function are still strict. For example: 1. count (1) ErrorHql code
"GetRecordCountById">
Select count (1) As recordCount from Km c
Where c. id =: id
2. count (c. *) Error
Hql code
"GetRecordCountById">
Select count (c. *) as recordCount from Km c
Where c. id =: id
3. The count (*) is correct.
Hql code
no repeated elements in the Set, the values of username and password cannot be the same. Change hql to string hql = "select new set (name, passwd) from users ";
7. Modify the default query result (query. List (). The result is not returned in the form of an object [] array and is returned in a custom type.
Custom class:
package COM. domain;
// Query. in the list (), the default object array is
Where clauseFilter the selected results to narrow down the selection range. If you do not name an alias for a persistent instance, you can directly use the attribute name to reference the attribute:
From person where name like 'Tom %'
The preceding hql statement has the same effect as the following statement:
From person as P where P. name like 'Tom %'
The composite attribute expression enhances the WHERE clause function, for example:
Fro
In NHibernate HQL can help us to turn into the final dependent database query script;Syntax is also very strong, suitable for the mainstream database, HQL does not support union, to take multiple table data can do two separate queries.IQuery query = nhibernatehelper.opensession () . CreateQuery ( @ "select p from Product as p,product as pp where p.nam
Org. hibernate. hql. ast. QuerySyntaxError: user is not mapped. [from user as u where u. userid =?]
This problem occurs mainly because the hibernate statement is incorrect.
User. hbm. xml:
Therefore, the hql statement is as follows:
String hql = "from User as u where u. userid =? "
Java hql case-when usageif (null = = Sorter) { hql.append ("ORDER by m.mdate desc,case if m.mealtime = ' Morning ' then ' 1 ' when m.mealtime = ' Noon ' Then ' 2 ' when m.mealtime = ' Night ' and ' 3 ' when m.mealtime = ' midnight ' then ' 4 ' End '); } Note:The meaning of the above HQL statement can be understood as:By mdate descending order, but also to meet when mealtime = ' morning ',
object is more expensive, it is generally recommended to use singleton mode)Then, create an action object that can be used at the user level through the session.Open Transaction Object trans = session. BeginTransaction ();The idea above and the SQL Server we used to use is a truth. Next look at the difference: When we used to deal with databases, we usually use SQL statements and then manipulate the database directly. and All operations in NH (increase and revise) All for" object ", If
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.