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"?>
"-//
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
Scenario Overview:I encountered such a problem in development:System and User relations: A user can belong to multiple systems, a system can have multiple users
Data structure: syssystem-system table, representing different system sysuser-user table, representing the user, attribute systems describes the Many-to-many relationship sys_user_system-relational table (not embodied as entity Class)
when the business logic is: Query a system under the user name contains "xbz" user, his
In the HQL statement, you cannot use limit to limit the number of bars that are displayed.
For example:
From User_info ORDER BY createdtime desc LIMIT 1
The number shown is not 1, but the number of bars you have queried, so the above notation is wrong.
How to solve the limit in HQL can not be used, and set the number of queries out of the collection, we should use the Setmaxresults (e) method to solve
select name,age,user_id from T_user JPQL: Object-oriented JPQL statements
Analysis:The first statement is used to execute a query against the T_user table tables, querying the name, age, and user_id three data columns;The second statement is used to execute a query against the user entity, querying the name, age, and UserId properties of the user entity.Third, HQLHQL (Hibernate query Language) is an object-oriented query, followed by a "table name", where the condition is followed by "fields i
.*;import org.hibernate.*;import org.hibernate.cfg.*;p ublic class Test {public static void main (string[ ] args) {//Here is the so-called establishment transaction session Session=new Configuration (). Configure (). Buildsessionfactory (). Opensession (); Transaction transaction=session.begintransaction (); TestTable testtable=new testtable (); listThe central idea of the entire file is to use the HQL statement to convert the query resu
Label:Question: How do I use MySQL functions in hibernate? 1.hibernate supports the original SQL statement query, using Session.createsqlquery () to create query objects; 2. How do I use the MySQL function in hql? You need to redefine Hibernate's dialect (dialect) to register your needs; The concept of a dialect (dialect) is mentioned here: Hibernate is still using SQL statements to perform database operations, although all relational databases supp
Session session = This.getsession ();
String hql = "from Tblfwxx FW where fw.title like?";
Query query = session.createquery (HQL);
Query.setstring (0, "% Yan er%");
Used above. " As a placeholder, and then set the value of the parameter one by one, using the SetString method of query. But this reduces the readability of the code.
So try to use "named parameter query" to solve these problems later
Repl
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.