Getting started 05 - Basic Data Inquiry
Using Hibernate is a simple matter, Java programming can use object operations to perform data queries, using a SQL HQL (Hibernate Query Language) to set the query condition, with SQL Different, HQL is a language with object-oriented inheritance, multi-purpose characteristics. Direct use of examples to see how to use Hibernate for database queries, before this, please add a few data in the database before this: New information in Hibernate
When querying data, we use the session's Find () method, and specify the HQL setting query condition, the result of the query is loaded back in the list object, you need to take them one by one, one The simplest example is as follows:
Hibernatetest.java
Import Onlyfun.caterpillar. *;
Import net.sf.hibernate. *;
Import net.sf.hibs. *;
Import java.util. *;
Public class hibernatetest {
Public static void main (string [] args) throws hibernateException {
SESSIONFACTORY sessionFactory = new configuration (). CONFIGURE (). BuildSessionFactory ();
Session session = sessionFactory.openSession ();
List users = session.find ("from user");
session.close ();
SessionFactory.Close ();
For (listiterator itrator = users.listiterator (); item.hasnext ();) {
User User = (user) iterator.next ();
System.out.println (user.getname ()
"/ N / TAGE:" user.getage ()
"/ n / tsex:" user.getSex ());
}
}
}
"from user" in find (), User refers to the User category. By mapping files, it will query the data in the USER table, which is equivalent to Select * from User in SQL, actually our User category It is located under ONLYFun.caterpillar, and Hibernate will automatically look at whether the package name in the Import is in line with the category name, you can also specify the package name directly, for example:
Session.Find ("from onlyfun.caterpillar.user");
The result of this program may be like this:
Log4j: warn no appenders could be bound for logger (net.sf.hibernate.cfg.environment).
Log4J: Warn Please Initialize The log4j system prot in.
Hibernate: select user0_.user_id as user_id, user0_.name as name, user0_.sex as sex, user0_.age as agomb user user0_caterpillar
Age: 28
SEX: M
MoMor
Age: 25
SEX: F
Bush
Age: 25
SEX: M
Becky
Age: 35
SEX: F
The query described above is the easiest, just queries all data from the data table, and the data obtained by Hibernate is back to the object, in order to match the needs of the program, we can also limit some queries. Conditions and only pass the fields we specified, for example:
List name = session.find ("SELECT User.Name from User AS User Where Age = 25");
For (listiterator itrator = names.listiterator (); item.hasnext ();) {
String name = (string) iterator.next ();
System.out.println ("Name:" Name);
}
In Find () HQL demonstrates the conditional qualified query, users as User take aliases for the User category, so we can use user.name to specify the table transfer field, where is equivalent to where clauses in SQL, we Limited the data of the query AGE equal to 25, this query is only one field, and the type is String, so the back-back list content is a String object, and a running example is as follows:
Log4j: warn no appenders could be bound for logger (net.sf.hibernate.cfg.environment).
Log4J: Warn Please Initialize The log4j system prot in.
Hibernate: SELECT User0_.name AS X0_0_ from user user0_ where (agn = 25)
Name: momor
Name: Bush
If you want to pass more than two fields, it is not a problem, come directly to see an example:
List results = session.find ("Select User.Name, User.age from User ASer WHERE Sex = 'F'");
For (Listiterator Iterator = Results.Listiterator (); item.hasnext ();) {
Object [] rows = (Object []) iterator.next ();
String name = (string) rows [0];
Integer Age = (Integer) ROWS [1];
System.out.println ("Name:" Name "/ N / T" AGE);
}
It is not difficult to see from the above program. When two or more fields are back, each Listiterator will return a data in an Object array, and we only specify an array index and convert to an appropriate type, you can obtain data. The result of a query is as follows:
Log4j: warn no appenders Could Be Found for logger (net.sf.hibernate.cfg.environment .log4j: war4j system prot portialize.
Hibernate: select user0_.name as x0_0_, user0_.age as x1_0_ from user user0_ where (sex = 'f')
Name: momor
25
Name: Becky
35
You can also use some functions in HQL to make results statistics, for example:
List results = session.find ("Select Count (*), AVG (User.Age);");
Listiterator itrator = results.listiterator ();
Object [] rows = (Object []) iterator.next ();
System.out.println ("Number of data:" rows [0] "/ N Average age:" rows [1]);
The results of a query are as follows:
Log4j: warn no appenders could be bound for logger (net.sf.hibernate.cfg.environment).
Log4J: Warn Please Initialize The log4j system prot in.
Hibernate: Select Count (*) AS X0_0_, AVG (user0_.age) AS x1_0_ from user user0_
Number of materials: 4
Average age: 28.25
What we will introduce some simple inquiry movements first, there will be a chance to introduce some advanced queries, if you want to know some HQL, you can take a look at Chapter 11 of the Reference Manual, and there is HQL Detailed explanation.