In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article shows you what the function of HQL query language is, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1. Get Hibernate Session object
two。 Write HQL statements
3. Call the createQuery method of Session to create a query object with the HQL statement as a parameter
4. If the HQL statement contains parameters, call the setXxx method of Query to assign values to the parameters
5. Call the list () or uniqueResult () method exclusive to Query to return the list of query results
A simple example:
The copy code is as follows:
@ SuppressWarnings ("deprecation")
Public class HibernateUtil {
Private static final SessionFactory sessionFactory
Static {
SessionFactory = new AnnotationConfiguration (). Configure (). BuildSessionFactory ()
}
Public static Session getOpenSession () {
Return sessionFactory.openSession ()
}
Public static Session getCurrentSession () {
Return sessionFactory.getCurrentSession ()
}
}
@ Entity
Public class Employee {
Private Integer id
Private String name
Private Integer age
@ Id
@ GeneratedValue
Public Integer getId () {
Return id
}
Public void setId (Integer id) {
This.id = id
}
@ Basic
Public String getName () {
Return name
}
Public void setName (String name) {
This.name = name
}
@ Basic
Public Integer getAge () {
Return age
}
Public void setAge (Integer age) {
This.age = age
}
Public String toString () {
Return "id:" + id + "" + "name:" + name + "" + "age:" + age
}
}
@ SuppressWarnings ("all")
Public class HQLDemo {
@ Test
Public void testHQL () {
Session session = HibernateUtil.getOpenSession ()
List employeeList = session.createQuery ("from Employee as e") .list
For (Employee e: employeeList)
System.out.println (e)
}
@ Test
Public void testHQLHasParameter () {
Session session = HibernateUtil.getOpenSession ()
List employeeList = session.createQuery ("from Employee as e where e.name =: personName") .setString ("personName", "xujianguo") .list ()
For (Employee e: employeeList)
System.out.println (e)
}
}
The from clause of the HQL query:
From is the simplest HQL statement and the most basic HQL statement, with the from keyword followed by the class name of the persistent class, such as:
The from Employee table name selects all instances from the Employee class
But what we often do is this:
The e of from employee as e is the alias of Employee, that is, the instance name, which is recommended.
The select clause of the HQL query:
The select clause is used to select the specified attribute or select an entity directly. Of course, the attribute selected by select must be the attribute contained in the persistent class after from, such as:
Select e.name from Employee as e select can select any property, that is, you can select not only the direct properties of the persistent class, but also the properties that the component properties contain, such as:
Aggregate function for select e.name.firstName from Employee as eHQL query:
Aggregate function:
Avg: calculate the average value of an attribute
Count: count the number of selected objects
Max: the maximum value of the statistical attribute
Min: the minimum of the statistical attribute value
Sum: calculates the sum of attribute values
Such as:
The copy code is as follows:
Select count (*) from Employee as e @ Test
Public void testHQLFunction () {
Session session = HibernateUtil.getOpenSession ()
System.out.println (session.createQuery ("select count (*) from Employee as e") .uniqueResult ()
}
Polymorphic query:
HQL queries not only all instances of the persistent class, but also all instances of subclasses of the class, provided there is an inheritance mapping.
The where clause of the HQL query:
The where clause is mainly used to filter the selected results and narrow the scope of selection, such as:
The copy code is as follows:
From employee as e where e.name like "xjg%" @ Test
Public void testHQLWhere () {
Session session = HibernateUtil.getOpenSession ()
List employeeList = session.createQuery ("from Employee as e where e.name like 'zhou%'") .list ()
For (Employee e: employeeList)
System.out.println (e)
}
Order by clause:
The query return combination can be sorted by any property of the class or component property, and you can specify ascending or descending order using the asc or desc keyword, such as:
From Employee as e order by e.name desc
Subquery:
In a subquery, there are query statements in the query statement, such as:
The copy code is as follows:
From Employee as e where e.age > (select p.age from Person as p) @ Test
Public void testHQLChildQuery () {
Session session = HibernateUtil.getOpenSession ()
List employeeList = session.createQuery ("from Employee as e where e.age > (select e1.age from Employee as E1 where e1.name = 'xujianguo')") .list ()
For (Employee e: employeeList)
System.out.println (e)
}
[code]
Named query:
HQL queries also support putting the HQL statements used in the query into the configuration file, rather than in the code, and using child elements in the elements of the Hibernate mapping file to define the named query, which simply specifies a name attribute and specifies the name of the named query, such as:
[code]
From Employee as e
A getNamedQuery (String name) method is provided in Session to create a Query object, and once you get the Query object, the rest of the work is the same as before.
The copy code is as follows:
@ Test
Public void testHQLNamedQuery () {
Session session = HibernateUtil.getOpenSession ()
List employeeList = session.getNamedQuery ("query") .list
For (Employee e: employeeList)
System.out.println (e)
}
HQL query statement
/ * *
*
, /
Package com.b510.example
Import java.util.Iterator
Import java.util.List
Import java.util.Map
Import org.hibernate.Criteria
Import org.hibernate.FetchMode
Import org.hibernate.Query
Import org.hibernate.Session
/ * *
*
* @ author XHW
*
* @ date 2011-6-18
*
, /
Public class HibernateTest {
/ * *
* @ param args
, /
Public static void main (String [] args) {
HibernateTest test = new HibernateTest ()
Test.where ()
Test.function ()
Test.update ()
Test.jiaoChaCheck ()
Test.innerJoin ()
Test.QBC ()
Test.leftOuterJoin ()
Test.rightOuterJoin ()
}
Public void where () {
/ / query using where
Session session = HibernateSessionFactoryUtil.getSessionFactory ()
.openSession ()
Session.beginTransaction ()
Query query = session
.createQuery ("from User where id not between 200 and 2000")
List list = query.list ()
For (User user: list) {
System.out.println (user.getId () + user.getUsername ())
}
/ / where clause is used in projection query
Query = session.createQuery ("select username from User where id=2")
List listname = query.list ()
For (String name: listname) {
System.out.println (name)
}
/ / in query
Query = session
.createQuery ("from User where username in ('Hongten','Hanyuan','dfgd')")
List listin = query.list ()
For (User user: listin) {
System.out.println (user.getId () + user.getUsername ())
}
/ / like query
Query = session.createQuery ("from User where username not like 'Hon%'")
List listlike = query.list ()
For (User user: listlike) {
System.out.println (user.getId () + user.getUsername ())
}
/ / null query
Query = session.createQuery ("from User where password is null")
List listnull = query.list ()
For (User user: listnull) {
System.out.println (user.getId () + user.getUsername ())
}
/ / and query
Query = session
.createQuery ("from User where password is not null and id"
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.