Hibernate framework study notes (2) --- query method of hibernate

Personal study record: hibernate framework study notes (2) -----hibernate query method


1. Hibernate query classification:

     The hibernate framework is a fully automated, object-oriented framework. Its query methods or statements can be divided into the following three types:

     1. hql query: hibernate query language, which is a unique query language of the hibernate framework, providing richer, more flexible and more powerful query capabilities

     2. Criteria query: Criteria is a completely object-oriented and extensible conditional query API. It is the core query object of the Hibernate framework that does not need to consider how the underlying database is implemented and how SQL statements are written. 

     3. Native sql query: It is inconvenient to use hql or criterion query for some complex query requirements, such as multi-table query, so a hibernate framework can still support native sql for query.


Second, the usage of three kinds of queries

        1. hql query:

         
//The encapsulated hibernate tool class is used to obtain the session
public class HibernateUtils {
	
	private static SessionFactory sf ;
	
	static{
		// get the configuration object
		Configuration conf = new Configuration().configure();
		//Get the session factory
		  sf = conf.buildSessionFactory();
		
	}
	
	public static Session openSession(){
		
		//create new session
	
		return sf.openSession();
		
	}
	
	public static Session getCurrentSession(){
		
		//Get the session of the current thread
		
		return sf.getCurrentSession();
		
	}
}


public void fun1(){
		
		Session session = HibernateUtils.openSession();
		Transaction tr = session.beginTransaction();
		//hql query
		//-------------------------
		//1, write hql statement
		String hql = "from Customer";//------->Complete query
		//2. Obtain the query object according to the statement
		Query query = session.createQuery(hql);
		//3. Obtain a collection or a single object according to the query object
		List<Customer> list = query.list();
		//-------------------------
		tr.commit();
		session.close();			 
		
		for(Customer c : list){
			System.out.println(c);
		}
	}
Note that the above hql specification statement should be select * from xx.xx.domain.Customer, and select * can be omitted for full query. If you want to query the number of rows in the table, you can use select count(*), and you cannot write the corresponding database table after from. Instead, you should write the complete persistent entity class name (object-oriented). If the entity class name is unique, the path can be omitted.
If it is just a simple query, in fact, there is no need to open a transaction and submit a transaction, because it does not involve the addition, deletion and modification of the database, the database will not change, and there is no need for shiwukongz

        
@Test
		public void fun2(){
			
			Session session = HibernateUtils.openSession();
			Transaction tr = session.beginTransaction();
			//hql query
			//-------------------------
			//1, write hql statement
			String hql = "from Customer where cust_id = 3";//-------> conditional query
			//2. Obtain the query object according to the statement
			Query query = session.createQuery(hql);
			//3. Obtain a collection or a single object according to the query object
			Customer c = (Customer)query.uniqueResult();
			//-------------------------
			tr.commit();
			session.close();			 
			
			System.out.println(c);
		
	}
		
		@Test
		public void fun3(){
			
			Session session = HibernateUtils.openSession();
			Transaction tr = session.beginTransaction();
			//hql query
			//-------------------------
			//1, write hql statement
			String hql = "from Customer where cust_id = ?";//-------> conditional query with placeholder
			//2. Obtain the query object according to the statement
			Query query = session.createQuery(hql);
			//query.setLong (0, 3L);
			// placeholder position and value
			query.setParameter(0, 3L);
			//3. Obtain a collection or a single object according to the query object
			Customer c = (Customer)query.uniqueResult();
			//-------------------------
			tr.commit();
			session.close();			 
			
			System.out.println(c);
		
	}
		
		@Test
		public void fun4(){
			
			Session session = HibernateUtils.openSession();
			Transaction tr = session.beginTransaction();
			//hql query
			//-------------------------
			//1, write hql statement
			String hql = "from Customer where cust_id = :cust_id";//-------> conditional query, with anonymous placeholder
			//2. Obtain the query object according to the statement
			Query query = session.createQuery(hql);
			//query.setLong (0, 3L);
			// placeholder position and value
			query.setParameter("cust_id", 2L);
			//3. Obtain a collection or a single object according to the query object
			Customer c = (Customer)query.uniqueResult();
			//-------------------------
			tr.commit();
			session.close();			 
			
			System.out.println(c);
		
	}

The above is a conditional query


@Test
		public void fun5(){
			//Paging query
			Session session = HibernateUtils.openSession();
			Transaction tr = session.beginTransaction();
			//hql query
			//-------------------------
			//1, write hql statement
			String hql = "from Customer";//------->Complete query
			//2. Obtain the query object according to the statement
			Query query = session.createQuery(hql);
			query.setFirstResult(0);//---->limit 0,3
			query.setMaxResults(3);
			//3. Obtain a collection or a single object according to the query object
			List<Customer> list = query.list();
			//-------------------------
			tr.commit();
			session.close();			 
			
			for(Customer c : list){
				System.out.println(c);
			}
		}

The above is a pagination query


 2. Criteria query:


/**
			 * criteria no statement query
			 */
			@Test
			public void fun6(){
				Session session = HibernateUtils.openSession();
				Transaction tr = session.beginTransaction();
				//--------------------
				Criteria criteria = session.createCriteria(Customer.class);//----->Query all
				List<Customer> list = criteria.list();
				//--------------------
				tr.commit();
				session.close();
				for(Customer c:list){
					System.out.println(c);
			}

criteria is completely object-oriented, it does not need to write any sql statement when querying, but calls its encapsulation method


Conditional query

  

  


Paging query


The total number of queries



3. Native sql query

There are two forms of basic query, one is to generate Object[] array collection, the other is to generate object collection, generally use the second



  

 


Conditional query is similar to hql query



Paging query

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325649312&siteId=291194637