Detailed explanation of hibernate hql syntax

Source: Internet
Author: User
Hibernate hql syntax and related foreign key Association

Hql

For example, for the Tuser class

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

Executing this statement will return records of the user and the user subclass.
Note: If the Tuser class has a foreign key, no error will be reported in the query results. However, if the foreign key in the results is null, a null pointer error will be reported during access!
Solution: Select alias. Attribute from class as alias. No alias. Attribute still reports error! // This method may not be able to solve the problem, but it will not be wrong.

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

Where statement, as can be omitted
Hql = "from user as user where user. Name = 'yyy'"; // user. name indicates the attributes of the class.

Hql = "from user where user. Name = 'yyy '";

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

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

You can also dynamically construct object instances in hql to encapsulate data.
(2) List list = session. createquery ("select new user (user. Name, user. Age) from Tuser as user"). List ();

Iterator it = List. iterator ();
While (it. hasnext ()){
User user = (User) it. Next ();
System. Out. println (user. getname ());
}
However, note that the user 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 user as user"

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

3. Update and delete objects

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

4. Grouping and sorting
Order by clause:
From user order by user. Name, user. Age DESC
Group by clause and having clause
"Select count (user), user. Age from 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 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 user where user. Name =? And user. age>? ");
Query. setstring (0, "Erica ");
Query. setinteger (1, 20 );

Fill in parameters by referencing placeholders:
String hql = "from user 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
(Org. hibernate. lazyinitializationexception: cocould not initialize proxy-No session exception. It may be that the hql statement does not perform a join Table query and the accessed attribute does not exist .)
User table: ID, name
Addresses table: user_id, addresses
(1) Natural join (inner join): inner join [fetch]
1. hql: from user U inner join fetch U. Addresses
SQL: Select * from user table U inner join addresses a on U. ID = A. user_id // U. ID = A. user_id has been configured in XML, so it can be omitted.
Fetch: After the addresses object is read, it is immediately filled into the foreign key (SET) corresponding to the user object.

2. hql: from user U inner join U. Addresses
Without fetch, each record in the returned result set is an array of objects. The array includes two objects: user and addresses, and the user object. addresses set is automatically filled by the addresses object.

(2) left join: left Outer Join [fetch]
Same as (1), the returned results are different.

(3) Right join: Right out join
Fetch is invalid because the user object may be null and cannot be filled. However, the records in the returned results are arrays.

(4) Cartesian intersection: Full join (rarely used)
Same as (3), the returned results are different.

(5) Cartesian set: permutation and combination: Form user, address

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.