Hibernate three ways of querying

Source: Internet
Author: User
Tags define array definition execution expression final functions null null

(i) HQL

Hql:hibernate qusery Language, if you're already familiar with it, you'll find it's very similar to SQL. But don't be fooled by appearances, HQL is object-oriented (OO, looking at each object with life's eyes, they are so alive). If you have some knowledge of Java and SQL statements, then HQL is a breeze for you, and you can use the time on the bus to master it. The following from several aspects of the gradual deepening: 1. The size of sensitive people know that SQL-92 query is insensitive to case, but in hql (it is OO), the names and properties of the object classes are case-sensitive (in accordance with Java programming syntax).



The HQL clause itself is case-insensitive, but the class name and property name appearing in it must be noted for case sensitivity: Select Cat.name from Cat as Cat and select Cat.name from Cat as Cat are the same but: select Cat.nam E from cat as Cat and select Cat.name from cat as cat are indeed different. 2. The simplest from statement: from EG. Cat It just simply returns all the eg. Example of cat, usually we will now be eg. Cat's individual name, because it may be used in the remainder of query (see the case in case of case sensitivity above), such as: from EG. Cat as cat here as can be omitted.



The above is just a single table query, the case of multiple tables is as follows: from EG. Cat, eg. Dogfrom eg. Cat as cat, eg. Dog as Dog3. Join related (inner) joinleft (outer) joinright (outer) Joinfull joinhql The same thing about these features in SQL. Here's a little topic about the features I've been using, and since I've been here today, , I would like to say a few of the characteristics of the above, but also a supplement to their own:



Suppose there are two tables: departments, employees, and some data below: Employee: ID Name depno 001 Jplateau 002 Jony 003 Camel 0 2



Department (Department): ID Name 01 Research and Development Department 02 marketing Department in Hibernate, we manipulate objects, so we manipulate departmental and employee classes.



















1). (inner) Joinselect employee.id as Id1,employee. Name as Name1,



Department.id as Id2,department. Name as name2 from employee as employee



 join  department as Department on employee. Depno=department.id (Note that the conditional statement I use on does not use where) what is the result of the execution? ID1 name1 id2 name2++++++++++++++++++++++++++++++++++++++001 Jplateau 01 Research and Development Department 002 Jony 01 Rd 2). Left (outer) Joinselect Empl Oyee.id as Id1,employee. Name as name1,department.id as id2,department. Nameas name2 from employee as employee left join Department as Department on employee. Depno=department.id So what should be the result of the execution? ID1 name1 id2 name2++++++++++++++++++++++++++++++++++++++001 Jplateau 01 Research and Development Department 002 Jony 01 R/d 003 Camel null NULL {That's when I want The number of records in a table is whichever, and the second table is populated with null} 3 when there is no corresponding record. Right (outer) Joinselect Employee.id as Id1,employee. Name as name1,department.id as id2,department. Nameas name2 from employee as employee right joins Department as Department on employee. Depno=department.id So what should be the result of the execution? ID1 name1 id2 name2++++++++++++++++++++++++++++++++++++++001 Jplateau 01 Research and Development Department 002 Jony 01 Research and Development Department NULL 02 marketing department {That's when I'm going to have a second table The number of records is the same, the first table does not have the corresponding record, fill null} 4. The SELECT statement is to determine which objects or objects you want to return from the query. Write a few examples: select EMployee Form employee As Employee Select Employee Form employee as employee where employee. Name like ' j% ' select employee. Name form employee as employee where employee. Name like ' j% ' select Employee.id as Id1,employee. Name as name1,department.id as id2,department. Nameas name2 from employee as employee right joins Department as Department on employee. Depno=department.id Select elements (employee. Name from employee as employee (do not understand what elements is doing?) Look at the description) and so on



5. The mathematical function Jdo currently does not seem to support such a feature. AVG (...), sum (...), min (...), Max (...) count (*) count (...), COUNT (distinct ...), count (all ...) is essentially the same as SQL Select Distinct Employee.Name from employee as Employee select COUNT (Distinct employee.name), count (employee) from employee as EMP Loyee 6. Polymorphism (don't know how to explain for a while?) The from Com.test.Animal as Animal not only obtains all Animal instances, but also can get all Animal subclasses (if we define a subclass cat) a more extreme example from Java.lang.Object as O can get instance 7 of all persistent classes. The WHERE statement defines the condition of the query statement, for example: from employee as employee where employee. Name= ' Jplateau ' from employee as employee where employee. Name like ' j% ' from employee as employee where employee. Name like '%u ' in the WHERE statement ' = ' can compare objects, such as: Select Animal from Com.test.Animal as animal where ANIMAL.NAME=DOG8. Most expressions in an SQL statement can be used in HQL: mathematical operators +,-, *,/binary comparison operators =, >=, <=, <>,!=, L IKE logical operations and, or, not string concatenation | | SQL scalar functions like upper () and lower () parentheses () indicate grouping in, between, are null JDBC in Parameters? Named Parameters:name,: start_date,: X1 (This should be another "?" WORKAROUND) SQL literals ' foo ', 1970-01-01, ' 10:00:01.0 ' Java public static final constants eg. Color.tabby other do not have to explain, here I just want to query the parameters of the problem explained: We know in the SQL to carry out the parameters of the query, we usually use PreparedStatement, in the statement to write a lot of "? , this method can also be used in hql, such as: List mates = Sess.find ("Select Employee.Name from Employee as employee" + "where employee." Name=? ", name,hibernate.string);(Description: Above using the Find method in the session, in the Hibernate API session overloaded a lot of find methods, it can satisfy your various forms of query) above is a parameter case, In this case, the parameter and the type of the definition parameter are introduced, and when you call another find method for multiple arguments, the latter two arguments are in the form of an array. There is another way to solve the problem above, JDO also has such a method, but and hibernate's performance is different, but their two bones are the same, such as: Query q = sess.createquery ("Select Employee.Name From employee as employee where employee. Name=:name "); Q.setstring (" Name "," Jplateau ");//define Iterator employees = q.iterate () when there are multiple parameters; 9. There is no difference between an order statement and an SQL statement, such as: Select Employee.Name from employee as employee where employee. Name like ' j% ' is employee.id desc (or ASC) 10. The GROUP BY statement also has no difference from the SQL statement, such as: Select Employee.name,employee. Depno from employEE as employee GROUP by employee. Depnoselect foo.id, AVG (Elements (foo.names)), Max (indices (foo.names)) from eg. Foo Foo GROUP by foo.id{note:you could use the elements and indices constructs inside a SELECT clause, even on databases WI Th no subselects.} Who can help me explain the above two sentences, thank you! 11. Subquery hibernate also supports subqueries, writing a few examples: from eg. Cat as FatCat where Fatcat.weight > (select AVG (cat.weight) from eg. Domesticcat cat) (ii) Condition query criteria  queries
。 The mathematical function Jdo currently does not seem to support such a feature. AVG (...), sum (...), min (...), Max (...) count (*) count (...), COUNT (distinct ...), count (all ...) is essentially the same as SQL Select Distinct Employee.Name from employee as Employee select COUNT (Distinct employee.name), count (employee) from employee as EMP Loyee 6. Polymorphism (don't know how to explain for a while?) The from Com.test.Animal as Animal not only obtains all Animal instances, but also can get all Animal subclasses (if we define a subclass cat) a more extreme example from Java.lang.Object as O can get instance 7 of all persistent classes. The WHERE statement defines the condition of the query statement, for example: from employee as employee where employee. Name= ' Jplateau ' from employee as employee where employee. Name like ' j% ' from employee as employee where employee. Name like '%u ' in the WHERE statement ' = ' can compare objects, such as: Select Animal from Com.test.Animal as animal where ANIMAL.NAME=DOG8. Most expressions in an SQL statement can be used in HQL: mathematical operators +,-, *,/binary comparison operators =, >=, <=, <>,!=, L IKE logical operations and, or, not string concatenation | | SQL scalar functions like upper () and lower () parentheses () indicate grouping in, between, are null JDBC inParameters? Named Parameters:name,: start_date,: X1 (This should be another "?" WORKAROUND) SQL literals ' foo ', 1970-01-01, ' 10:00:01.0 ' Java public static final constants eg. Color.tabby other do not have to explain, here I just want to query the parameters of the problem explained: We know in the SQL to carry out the parameters of the query, we usually use PreparedStatement, in the statement to write a lot of "? , this method can also be used in hql, such as: List mates = Sess.find ("Select Employee.Name from Employee as employee" + "where employee." Name=? ", name,hibernate.string);(Description: Above using the Find method in the session, in the Hibernate API session overloaded a lot of find methods, it can satisfy your various forms of query) above is a parameter case, In this case, the parameter and the type of the definition parameter are introduced, and when you call another find method for multiple arguments, the latter two arguments are in the form of an array. There is another way to solve the problem above, JDO also has such a method, but and hibernate's performance is different, but their two bones are the same, such as: Query q = sess.createquery ("Select Employee.Name From employee as employee where employee. Name=:name "); Q.setstring (" Name "," Jplateau ");//define Iterator employees = q.iterate () when there are multiple parameters; 9. There is no difference between an order statement and an SQL statement, such as: Select Employee.Name from employee as employee where employee. Name like ' j% ' is employee.id desc (or ASC) 10. The GROUP BY statement also has no difference from the SQL statement, such as: Select Employee.name,employee. Depno from EmployeE As employee GROUP by employee. Depnoselect foo.id, AVG (Elements (foo.names)), Max (indices (foo.names)) from eg. Foo Foo GROUP by foo.id{note:you could use the elements and indices constructs inside a SELECT clause, even on databases WI Th no subselects.} Who can help me explain the above two sentences, thank you! 11. Subquery hibernate also supports subqueries, writing a few examples: from eg. Cat as FatCat where Fatcat.weight > (select AVG (cat.weight) from eg. Domesticcat cat) (ii) Condition query criteria  queries
Criteria criteria = Session.createcriteria (Tuser.class); Criteria.add (Expression.eq ("name", "Erica"); Criteria.add ( Expression.eq ("Sex", New Integer (1));

(iii) Native SQL statement queries




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.