Basic usage of hql

Source: Internet
Author: User
Basic usage of hql

1. Object Query
String hql = "from Tuser ";

Executing this statement will return records of Tuser and Tuser sub-classes.

Hql = "from Java. Lang. Object"
Records of all database tables in the database are returned.

Where statement
Hql = "from Tuser as user where user. Name = 'yyy '";

The same can be omitted for.
Hql = "from Tuser user where user. Name = 'yyy '";

In the WHERE clause, we can set conditions through comparison operators, such:
=, <>,>, <, >=, <=, Between, not between, in, not in, is, like, and so on.

2. query attributes
List list = session. createquery ("Select User. Name, user. Age from Tuser as user"). List ();

You can also dynamically construct object instances in hql to encapsulate data.
List list = session. createquery ("select new Tuser (user. Name, user. Age) from Tuser as user"). List ();
Iterator it = List. iterator ();
While (it. hasnext ()){
Tuser user = (Tuser) it. Next ();
System. Out. println (user. getname ());
}
However, note that the Tuser object only encapsulates the name and age attributes and does not assign values to other statuses. Therefore, it cannot be used for update operations.

You can also use statistical functions in the select clause of hql.
"Select count (*), min (user. Age) from Tuser as user"

You can also use the distinct keyword to delete duplicate records.
Select distinct user. name from Tuser as user;

3. Update and delete objects
In hibernate 2, you must first query the object and set the attribute before saving it.

Hibernate 3 provides more flexible methods (bulk delete/update)
Update:
Query query = session. createquery ("Update Tuser set age = 18 where id = 1 ");
Query.exe cuteupdate ();
Delete:
Session. createquery ("delete Tuser where age> = 18 ");
Query.exe cuteupdate ();

4. Grouping and sorting
Order by clause:
From Tuser user order by user. Name, user. Age DESC
Group by clause and having clause
"Select count (user), user. Age from Tuser user group by user. Age having count (User)> 10"

5. Parameter Bonding
By sequential placeholders? To fill in the parameters:
1) Fill in hibernate 2 through the session. Find Method
Session. Find ("from Tuser user where user. Name =? "," Erica ", hibernate. String );
Multiple parameters:
Object [] ARGs = new object [] {"Erica", new INTEGER (20 )};
Type [] types = new type {hibernate. String, hibernate. Integer };
Session. Find ("from Tuser user where user. Name =? And user. Age =? ", ArgS, types );

2) Fill in parameters through the query interface:
Query query = session. createquery ("from Tuser user where user. Name =? And user. age>? ");
Query. setstring (0, "Erica ");
Query. setinteger (1, 20 );

Fill in parameters by referencing placeholders:
String hql = "from Tuser where name =: Name ";
Query query = session. createquery (hql );
Query. setparameter ("name", "Erica ");

You can even encapsulate a query condition as a JavaBean
Class userquery {
Private string name;
Private integer age;
// Getter and setter
}
String hql = "from Tuser where name =: name and age =: Age ";
Query query = session. createquery (hql );
Userquery uq = new userquery ();
Uq. setname ("Erica ");
Uq. setage (New INTEGER (20 ));

Query. setproperties (uq); // will call the getter?
Query. iterate ();

6. Joint Query
You can also use inner join, left Outer Join, right out join, and full join.
Permutation and combination: Form Tuser and taddress

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.