Hibernate Learning (query), hibernate Learning (
Data Query is a highlight of hibernate. hibernate provides a variety of query methods for programmers, including the following:
1. hibernate language query, that is, the hql query we need to talk about today. This query is fully object-oriented, and the query statement is encapsulated as an object for operation. Maintain the database in line with the object-oriented thinking.
2. hibernate standard query: (criteria query) encapsulate query statements into objects for operations.
3. Original-class SQL query: The standard SQL language is used directly for query.
Hql query all
Before learning to query hql, I insert some records into the userinfo table:
// Parse hibernate. cfg. xml file Configuration cfg = new Configuration (). configure (); // create SessionFactory (create a connection pool) SessionFactory factory = cfg. buildSessionFactory (); // create sessionSession session = factory. openSession (); Transaction transaction = session. beginTransaction (); for (int I = 0; I <80; I ++) {UserInfo info = new UserInfo (); info. setUserName ("test" + I); info. setUserPass (String. valueOf (Math. random () * 1); session. save (info);} transaction. commit ();
Here, I inserted 80 records in the userinfo table at a time. Use hql to query all data:
String hql = "from UserInfo";Query query = session.createQuery(hql);List<UserInfo>lists = query.list();for (UserInfo userInfo : lists) { System.out.println(userInfo.getUserName());}
Here, the query does not directly omit this statement as the SQL statement contains "select *". In fact, hql and SQL are the same, but everything is an object-oriented query, replace the name of the table to be queried with the corresponding object class name, and replace the field to be queried with the attribute of the corresponding object class.
Hql condition Query
1. query records with id> 10 and id <20
String hql = "from UserInfo where userId> 10 and userId <20"; // or String hql = "from UserInfo where userId between 10 and 20 ";
2. sort by userId in descending order
String hql = "from UserInfo order by userId desc";
3. query using parameter placeholders
String hql = "from UserInfo where userId between ? and ? order by userId desc";Query query = session.createQuery(hql);query.setInteger(0, 30);query.setInteger(1,40);List<UserInfo>lists = query.list();
4. Bind meaningful parameter placeholders
String hql = "from UserInfo where userId between :begin and :end order by userId desc";Query query = session.createQuery(hql);query.setInteger("begin", 20);query.setInteger("end",40);List<UserInfo>lists = query.list();
5. query records with the largest userId Value
String hql = "select max(userId) from UserInfo";Query query = session.createQuery(hql);List<UserInfo>lists = query.list();Object result = query.uniqueResult();System.out.println(result);
We can see that only one record is queried here, So query. uniqueResult (); is used for query.
6. hql paging Query
The query starts from 3rd records and ranges from 5 Records.
String hql = "from UserInfo";Query query = session.createQuery(hql);query.setFirstResult(3);query.setMaxResults(5);List<UserInfo>lists = query.list();
7. Only query certain fields
String hql = "select new com.mydb.entity.UserInfo(userId,userPass) from UserInfo";Query query = session.createQuery(hql);List<UserInfo>lists = query.list();
Note that the constructor of two parameters is used here. Therefore, you must add the constructor of these two parameters to the UserInfo object class.
Or you can write it like this:
String hql = "select userId,userPass from UserInfo";Query query = session.createQuery(hql);List lists = query.list();for (Object object : lists) { Object[]objects = (Object[]) object; System.out.println(objects[0]+"----++++"+objects[1]);}
SQL query
Query all data in the userinfo table
String sql = "select * from userinfo";SQLQuery sqlQuery = session.createSQLQuery(sql);sqlQuery.addEntity(UserInfo.class);List<UserInfo> lists = sqlQuery.list();for (UserInfo userInfo : lists) { System.out.println(userInfo.getUserName());}
Note: sqlQuery. addEntity (UserInfo. class) is the type converted from object after hibernate query is added.
Now, you can learn about the hibernate query.