Talking about the HQL and QBC query in Hibernate

Source: Internet
Author: User
Tags expression one table

The first contact with Hibernate was in 2004, when with a disturbed and uneasy mood to learn this technology. After several years of use and research, but also accumulated some experience, the following HQL and QBC query to talk about my work from some of the summary.

This article will not talk about what is hibernate, what is ORM, and how to use HQL and QBC. The purpose of this article is to make everyone have a new understanding of the most commonly used, but also the most extensive database dealing with the two ways.

Well, if you do not know hibernate, elephants suggest you first to learn more about this article, if you have been the master of this area, can turn off the browser, do not because of my humble opinion, let you to the elephant, for personal attack.

HQL and QBC are query retrieval methods, at the beginning, I have been using the HQL, because previously used are all SQL, think this thing and SQL almost, start quickly. Later, with QBC, because QBC is an object-oriented query based on interfaces and classes, the code becomes clear and neat.

The following is the query user table, with ID 2, age equals 21, and the two query statements with the first name starting with J, their results are the same, just different ways of behaving.

HQL:

Query Query = Session
. CreateQuery ("from User u where u.id = 2 and u.age = ampersand and u.name like ' j% '");
List List = Query.list (); QBC:
Criteria criteria = Session.createcriteria (User.class);
List List = Criteria.add (EXPRESSION.EQ ("id", 2)). Add (
Expression.eq ("Age"). Add (Expression.like ("name", "j%"))
. List ();

If the query is more complex, you need to correlate more than one table, the above HQL statement will appear very complex, more difficult to read. For QBC, need to add a createcriteria (), to return a new criteria, such as the User table and Account Table Association, need to be based on the account number of account number, account opening time, amount of information such as query, can be written in the following form:

List List = Criteria.add (EXPRESSION.EQ ("id", 2)). Add (
Expression.eq ("Age"). Add (Expression.like ("name", "j%"))
. Createcriteria ("account", "a"). Add (
Expression.eq ("a.account_id", 112546)). Add (
Expression.eq ("A.start_date", "2008-8-30")). Add (
      Expression.eq ("A.money_sum", 1000)). List ();

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.