Some examples of Hibernate's HQL query

Source: Internet
Author: User

Hibernate is equipped with a very powerful query language, that is, HQL (Hibernate query Language), hql look like SQL, but just similar to the syntax structure, HQL is an object-oriented query, he can understand the concepts of inheritance, polymorphism.

HQL are case-sensitive, and SQL statement keywords (such as SELECT, From,where, and so on) are case insensitive.

Suppose we now have two tables, the book, and the Category:

Requirement 1: Query all titles in the Book Table:
Session session=hibernateutil.getsession (); // The list () method returns the result of the query // The type of the returned result is determined by the column of the query, where the Name property is queried, because the Name property is a string type, and all the resulting collection is a string type List<string> list=session.createquery ("Select name from book"). List ();  for (String s:list) {    System.out.println (s);}
Requirement 2: Query the Book table for all titles and authors (query multiple columns):

Method 1:

Session session=hibernateutil.getsession ();             // The list () method returns the    result of the query // A collection of arrays is returned when querying multiple properties, where name, and author are all string types, so the resulting list<object[]> list = Session.createquery ("Select Name,author from Book"). List ();  for (object[] o:list) {    System.out.println (o[0]+ "----" +o[1]);}    

Method 2:

1. In the book entity class, create a new constructor with the parameter name and author, (be careful not to forget the parameterless constructor);

2:

Session session=hibernateutil.getsession ();     // The list () method returns the result of the query and returns a collection of book types.
If you want to query all the book columns, the HQL statement is written directly as "from book". cannot be written as "select * from book".
Aliases can also be used in HQL statements, if aliases can also be written, "Select B from book B";
The following statement can also be writtenas follows: Select new Book (B.name,b.author) from book B;

list<book> list = Session.createquery ("Select New Book (Name,author) from book"

for (book book:list) {System.out.println (book);}

Printing results are:

Book [Id=0, Name= Ding Lu Kee, price=0.0, Author= Jin Yong, pubdate=null, Category=null]
Book [Id=0, Name= Bucket, price=0.0, author= potatoes, pubdate=null, category=null]
Book [Id=0, Name= engulfing the stars, price=0.0, author= tomato, pubdate=null, category=null]
Book [Id=0, Name= Liang Jian, price=0.0, Author= Liang, Pubdate=null, Category=null]
Book [Id=0, Name= evil awe-inspiring, price=0.0, author=-eared, Pubdate=null, Category=null]

Requirement 3: Conditional query, query all book of 2<id<5:
Session session=hibernateutil.getsession ();     // The list () method returns the result of the query, which returns a collection     of book types // because the ID here is of type int, you can also write the Setparameter method as Setinteger, the first parameter: 0 for the first question mark list<book> list = Session.createquery ("From book where ID <? and ID;? "). Setparameter (0, 5). Setparameter (1, 2). List ();    for (book book:list) {         System.out.println (book);  }

Printing results are:

Book [id=3, Name= engulfing the stars, price=27.35, author= tomato, pubdate=2016-03-06 09:25:41.0, [email protected]
Book [id=4, Name= Liang Jian, price=42.35, Author= Liang, pubdate=2016-03-06 09:25:41.0, [email protected]

Requirement 4: Query all book with category "Fantasy class":
list<category> list = Session.createquery ("from Category where name =: Name"). Setparameter ("name", "Fantasy Class"). List (); Set<Book> books = list.get (0). Getbooks (); Iterator<Book> it =while (It.hasnext ()) {          System.out.println (It.next ());  }

Printing results are:

Book [id=2, Name= Bucket, price=22.35, author= potatoes, pubdate=2016-03-06 09:25:41.0, [email protected]
Book [id=3, Name= engulfing the stars, price=27.35, author= tomato, pubdate=2016-03-06 09:25:41.0, [email protected]

Demand 5: Paging query:
list<book> list = Session.createquery ("from book")                          . Setfirstresult (0)// start recording the following table ( currentPage-1) *pagesize)                           . Setmaxresults (3)// Set the number of records per page pageSize                            for  (book book:list) {         System.out.println (book);  }

Printing results are:

Book [Id=1, Name= Ding Lu Kee, price=12.35, Author= Jin Yong, pubdate=2016-03-06 09:25:41.0, [email protected]
Book [id=2, Name= Bucket, price=22.35, author= potatoes, pubdate=2016-03-06 09:25:41.0, [email protected]
Book [id=3, Name= engulfing the stars, price=27.35, author= tomato, pubdate=2016-03-06 09:25:41.0, [email protected]

If the parameter of Setfilrstresult () is changed to 3: It will be displayed:

Book [id=4, Name= Liang Jian, price=42.35, Author= Liang, pubdate=2016-03-06 09:25:41.0, [email protected]
Book [Id=5, Name= evil awe-inspiring, price=12.35, author=, pubdate=2016-03-06 09:25:41.0, [email protected]

Some examples of Hibernate's HQL query

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.