Hibernate single Table query summary (top)
build the environment no longer repeat, interested can refer to the previous blog--– Hibernate environment (link) One, data preparation:
The table contains only three fields-name, age, score, Hibernate.cfg.xml file is already configured in the previous BOVENRI, insert data directly here
public void Testreadlydata () {sessions session
= Hbnutils.getsession ();
try {
session.begintransaction ();
for (int i = 1; I <= i++) {
Student Student = new Student ("Zhang" + i + "three", i +, + i);
Session.save (student);
}
Session.gettransaction (). commit ();
catch (Exception e) {
e.printstacktrace ();
Session.gettransaction (). rollback ();
}
Execute the above statement and the data is ready.
second, native SQL statement query
Core Code
String sql = "SELECT * from T_student";
Perform action
list<student> list = session.createsqlquery (sql)
. addentity (student.class). List ();
Session.createsqlquery (SQL) query is a set of key-value pairs (map), to be converted to the desired collection of objects, need to perform addentity (student.class) mapping
Complete Code
/**
* Test single Table query of the original query *
* *
@Test public
void Testquerysingletable_sql () {
Hbnutils.getsession ();
try {
session.begintransaction ();
String sql = "SELECT * from T_student";
Perform action
list<student> list = session.createsqlquery (sql)
. addentity (student.class). List ();
for (Student stu:list) {
System.out.println (stu);
}
Session.gettransaction (). commit ();
catch (Exception e) {
e.printstacktrace ();
Session.gettransaction (). rollback ();
}
third, HQL inquiry
--The full name is Hibernate query Language, which is characterized by object-oriented encapsulation of query conditions
Core Code
String hql = "from Student";
list<student> list = Session.createquery (HQL). List ();
The student here is the object name, not the table name, and the HQL internally has encapsulated the result of the query into the specified object
Four, qbc--query by criteria, through the standard query
Core code
list<student> list = Session.createcriteria (student.class). List ();
As you can see, the QBC is pure to face the object, concise to do not write query statements
QBC Retrieval Steps:
1. Call the Createcriteria () method of the session to create a criteria object.
2. Set the query conditions. The restrictions class provides a series of static methods for setting query conditions.
These static methods return the criterion instance, and each criterion instance represents a query condition.
The Add () method of the criteria is used to join the query criteria.
3. The list () method that invokes the criteria executes the query statement. This method returns the query result of the list type and holds the persisted object that matches the query criteria in the list collection.
Order of native SQL queries
Core code
String sql = "SELECT * from T_student ORDER by t_age DESC";// default to ascending ASC
//Perform action
list<student> List = SE Ssion.createsqlquery (SQL). Addentity (Student.class). List ();
Sorting of HQL queries
Core code
The Student here is the class name
String hql = "from Student to T_score desc";
list<student> list = Session.createquery (HQL). List ();
Interestingly, the following three results are consistent ... "This is to say that--score is a property of a class, T_score is a field of a table."
String hql = "from Student ORDER by t_score Desc";
String hql = "from Student s ORDER by s.score Desc";
String hql = "from Student ORDER by score Desc";
vii. QBC Query Results ordering
Core code
list<student> list = Session.createcriteria (Student.class)
. AddOrder (Order.desc ("Age")). List ();
* * Eight, HQL query set Dynamic parameters "1--placeholder form"
**
Query is often accompanied by query conditions, the above query are all query
Core Code
String hql = "from Student where >?" And score <? ";
list<student> list = Session.createquery (HQL). Setinteger (0). setdouble (1, 90.0). List ();
According to HQL's grammar rules, when a placeholder is used, the setting method of the corresponding type is called when the parameter is set Setinteger (0, 15), the first argument is the position of the placeholder, and the second is its value, and it is obvious that the disadvantage of this is that it is easy to confuse the location with too many arguments
Nine, HQL query set dynamic parameter "2--alias form"
Core Code
String hql = "from Student where age >:myage and score <:myscore";
list<student> list = Session.createquery (HQL). Setinteger ("Myage"). SetDouble ("Myscore", 90.0). List ();
The advantage of the alias is that it is not confusing, its syntax format is to require the alias must start with a colon, set the parameter still need Setinteger, setdouble set the corresponding type
10, hql query set dynamic parameters "3--Alternative placeholder"
Core Code
String hql = "from Student where"? and score;? ";
list<student> list = Session.createquery (HQL). Setparameter (0)
. Setparameter (1, 90.0). List ();
This differs from the previous setting placeholder in that the setparameter is used uniformly when setting the parameters, and it is important to note that the score here is a double, and the arguments must be of type double, 90 is different from 90.0.
11, hql query set dynamic Parameters "4--another category name"
Core code
String hql = "from Student where age >:age and score <:score";
list<student> list = Session.createquery (HQL). Setparameter ("Age")
. Setparameter ("Score", 90.0). List () ;//type must match exactly
The syntax is exactly the same as the alias, which is the alias, and changes only the method that sets the parameter