HQL statement and criteria query

HQL statement

HQL - > hibernate query language

Select/update/delete...... from ...... where ...... group by ...... having ...... order by ...... asc/desc

Connection query: Select/update/delete... from... join fetch where... group by... having... order by... asc/desc

Object oriented: form class name where property name = order by property name; class name and property name are case sensitive

select * from dept == from Dept	  [Class name]

Method:
Create query object

Query query = session.creatQuery(hql);

Get the object from the query:

query.list();//Get all object sets at once
query.iterate();Send out n+1 Bar statement,Get an object every time you send it
query.uniqueResult();//Get unique object

Paging:

query.setFirstresult();//
query.setMaxResult();//

Using a Query object

package hibtest1;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import entity.Dept;

public class HibernateTest {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/**
		 * list()Method to execute sql statement
		 */
		Configuration conf=null;
		SessionFactory sf=null;
		Session session=null;

		try {
			conf=new Configuration().configure();//read configuration file
			sf=conf.buildSessionFactory();//Create a factory
			session=sf.openSession();

			String hql ="from Dept";

			Query query = session.createQuery(hql);
			List<Dept> deptList = query.list();

			for(Dept d : deptList){
				System.out.println(d.getDname());
			}
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}	
	}
}

criteria query

Criteria CRI = session. Createculture (User. Class); / / query all User objects

//Set condition, return a Criterion condition object

 Restrictions.eq("name", name);
	Restrictions.eq()compare==
	Restrictions.in()Within limits
	Restrictions.ilike()String pattern matching
	Restrictions.and()logic
	Restrictions.isEmpty()aggregate

//Add query criteria

cri.add(Restrictions.eq("name", name));

Sorting is based on the Order object in criterian

cri.addOrder(Order.asc("salary"))//Sort,

paging

cri.setFirstResult(0)//Page from
cri.setMaxResults(2)//How many pieces of data per page

//Get query collection

cri.list();

//Query unique objects

uniqueResult()

Associate / establish internal connection / / parameters: table name and alias only support internal connection and urgent left external connection.

createCriteria()/createAlias("dept","d")

Projection, get part of column projection interface, projects class supports Criteria projection

setProjection()
//Parameters of projection, get deptName column
	Property.forName("deptName")
//Get column set
	Projections.projectionList().add(Property.forName("deptName1")).add(Property.forName("deptName2"))

Grouping
In Criteria, projection is used to implement group statistics. The projects class provides aggregate function query.
groupProperty() grouping
rowCount() statistics records
avg() statistical average

	@Test
	public void criteriaTest() {
		Session session = null;

		try {
			session = HibernateUtil.currentSession();

			Criteria cri = session.createCriteria(Dept.class);

			// Criterion criterion=Restrictions.eq("location", "Henan"); / / condition object
			//
			// Criterion criterion1=Restrictions.like("dname", "%R%");

			// cri.add(criterion).add(criterion1); / / add query criteria

			cri.add(Restrictions.ilike("dname", "r", MatchMode.ANYWHERE)).createCriteria("emps");

			// cri.createAlias("emps", "es"); / / Association query, alias es

			List<Dept> ds = cri.list();
				
			//														Add content in condition tool class column column sort content location project total group unique result
			// Long count =(Long) session.createCriteria(Dept.class) .add(Restrictions.ilike("dname", "r", MatchMode.ANYWHERE)).addOrder(Order.asc("deptno")).setProjection(Projections.rowCount()).uniqueResult();

			for (Dept dept : ds) {
				System.out.println(dept.getDname());
			}

			// System.out.println(count);
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession();// Free state: possible in memory, not in session, database
		}
	}

Tags: Session Hibernate Java SQL

Posted on Fri, 01 Nov 2019 14:02:47 -0700 by artic