Hibernate hql Search (query) method

Source: Internet
Author: User

HQL (Hibernate query Language) is an object-oriented query language that is very similar to SQL. In Hibernate, HQL is the most widely used method of retrieval.

Has the following frequently used functions:

(1) In the query statement, can set various conditions

(2) Support checking out the object part of the property, is not in the SQL statement *, but query the object we want to query

(3) Support connection query

(4) Support paging query

(5) Support sub-query

(6) Support dynamic binding of parameters

(7) Support packet query, can use Having,group by

(8) provides a grouping function (built-in aggregation function, sum (), COUNT (), AVG (), Max (), Min (), g)

(9) Ability to call itself to define SQL functions

The Find () method and the query interface of the session support the HQL retrieval method, in particular, the find () method is canceled in Hibernate3, the Find () method does not have the ability to dynamically bind the parameters,

retrieving and querying is a meaning that in object-oriented retrieval is much more said than in SQL queries. According to the habit to understand can .

The query interface is the true interface of the HQL query, and it is sufficient to understand it.

Let's look at an example first:

Package Com.lanhuigu.hibernate.test;import Java.util.list;import Org.hibernate.query;import org.hibernate.Session; Import Org.hibernate.sessionfactory;import Org.hibernate.transaction;import org.hibernate.cfg.Configuration; Import Com.lanhuigu.hibernate.entity.customer;public class Testhibernatejiansuo {public static void main (string[] args {Configuration CFG = new configuration (). Configure (); Sessionfactory sessionfactory = Cfg.buildsessionfactory (); Session session = Sessionfactory.opensession (); Transaction tr = session.begintransaction ();//1. Create a Query object query query = Session.createquery ("From Customer where name=:v _name ");//Do not habitually add SELECT//2. Dynamically bound parameters query.setstring (" V_name "," Test "),//3. Run SQL to return query results list List = Query.list ();//4. Output for (int i=0;i<list.size (); i++) {Customer customer = (customer) list.get (i); System.out.println (Customer.getname ());} 5. Transaction submission Tr.commit ();//6. Close Sessionsession.close ();}}
Operation Result:

Hibernate:select customer0_.id as id1_0_, customer0_.name as name2_0_, customer0_. EMAIL as email3_0_, customer0_. PASSWORD as password4_0_, customer0_. PHONE as phone5_0_, customer0_. ADDRESS as address6_0_, customer0_. SEX as sex7_0_, customer0_.is_married as is8_0_, customer0_. DESCRIPTION as descript9_0_, customer0_. IMAGE as image10_0_, customer0_. BIRTHDAY as birthda11_0_, customer0_. Registered_time as registe12_0_, customer0_. Home_province as home13_0_, customer0_. Home_city as home14_0_, customer0_. Home_street as home15_0_, customer0_. Home_zipcode as home16_0_, customer0_. Comp_province as comp17_0_, customer0_. Comp_city as comp18_0_, customer0_. Comp_street as comp19_0_, customer0_. Comp_zipcode as comp20_0_ from CUSTOMERS customer0_ where customer0_.name=?

Test

Example analysis of the query interface supported by the HQL method of running the procedure:

(1) Create a Query object. object includes query SQL. The SQL includes the named number of parameters V_name.

(2) Dynamic binding of the parameters. Set the parameters of the HQL according to the parameter naming method provided by the query interface. For example, query.setstring ("V_name", "Test");

(3) Run the query statement. Returns a list collection that holds the persisted objects that match the criteria.

Like what. When you call Query.list (), run SQL to return the collection of customer persisted objects.

(4) Support chain style. is to connect the connected code together, to be concise and clear. Like what:

Integrate the above code query sections such as the following:

List List = Session.createquery ("from Customer C where name=:v_name")    . SetString ("V_name", "Test")    . List ();
Run the same query effect.

Hibernate hql Search (query) method

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.