eclipse learning (Chapter 3: Hibernate in ssh) - 9 Standard query in Hibernate

Posted by ultrus on Sun, 30 Jan 2022 05:23:16 +0100

Foreword (it is suggested to have a look at the version gap)

This article refers to https://www.w3cschool.cn/hibernate/ugov1ie8.html A practice log made after.
If you use the old version directly, there is no problem.
Let me give you the simplest example. You can create it directly. If so, you can add parameters to it.

Criteria c = session.createCriteria(Address.class);
List<Address> l = c.list();

But if you change the version, the hibernate version is in Hibernate 5 2 after that, the abandoned sign will appear, which should be changed to the following.

	//Create CriteriaBuilder object
	CriteriaBuilder bulider = session.getCriteriaBuilder();
	//Create a CriteriaQuery object corresponding to the entity class
	CriteriaQuery<Address> criteriaQuery = bulider.createQuery(Address.class);
	//Set the condition through the CriteriaQuery object. Here, set the root condition
	Root<Address> addressRoot = criteriaQuery.from(Address.class);
	//Create query statement through session
	Query q = session.createQuery(criteriaQuery);
	//Get query results
	List<Address> a = q.getResultList();

If you use an deprecated method, this output will appear

WARN: HHH90000022: Hibernate's legacy org.hibernate.Criteria API is deprecated; use the JPA javax.persistence.criteria.CriteriaQuery instead

I would like to say that this standard query is used for query processing

Standard query

In fact, it is to extract general query conditions into methods and replace the process of writing hql. In fact, they are very similar. But I think writing a hql is actually better than writing this. At least a few lines of code are omitted

Some examples of standard queries

1. Single condition query (the new method is used here)

At present, I can only get the new method here. Sorry, the strength is not enough and I can't get it. In fact, I can get some more examples, but it's OK to look at the website https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#criteria-typedquery-entity

List<Address> addressList = null;
		try {
			transaction = session.beginTransaction();
			
			//Get CriteriaBuilder through session	
			CriteriaBuilder builder = session.getCriteriaBuilder();
			//Create query statement through bulider
			CriteriaQuery<Address> crQuery = builder.createQuery(Address.class);
			//Create root condition of query statement
			Root<Address> addressRoot = crQuery.from(Address.class);	
			//Setting query body in query statement
			crQuery.select(addressRoot);
			
			//The following are some treatments according to different situations
			//Query if studentId is greater than 6, gt is greater than
//			crQuery.where(builder.gt(addressRoot.get("studentId"), 6));
			
			//Query if studentId is less than 7, lt is less than
//			crQuery.where(builder.lt(addressRoot.get("studentId"), 7));
			
			//Query if studentId is equal to 6 and equal is equal to
//			crQuery.where(builder.equal(addressRoot.get("studentId"), 6));
			
			//Fuzzy query addressDetail is a column starting with province A. like is a fuzzy query, which is generally the same as% and%_ Use together
//			crQuery.where(builder.like(addressRoot.get("addressDetail"), "A province%");
			
			//The fuzzy query addressDetail is not the column beginning with province A. like is a fuzzy query, which is generally the same as% and%_ Use together
//			crQuery.where(builder.notLike(addressRoot.get("addressDetail"),"A province%");
			
			//Query the column with studentId between 6 and 7, and the obtained interval is: 6 < = x < = 7
//			crQuery.where(builder.between(addressRoot.get("studentId"), 6, 7));
			
			//Query the column whose addressDetail is not empty
//			crQuery.where(builder.isNotNull(addressRoot.get("addressDetail")));
			
			//Query columns with empty addressDetail
			crQuery.where(builder.isNull(addressRoot.get("studentId")));
			
			//Using session to execute custom query statements
			Query q = session.createQuery(crQuery);
			//Get query results
			addressList = q.getResultList();
		
			transaction.commit();

2. Single condition query (using deprecated method)

In the old method, this is a single condition processing. Of course, if you write two, it means and. It can also be used as a multi condition and processing, but it is recommended to use the next one

List<Address> addressList = null;
		try {
			transaction = session.beginTransaction();
			//Create Criteria according to the corresponding entity class
			Criteria criteria = session.createCriteria(Address.class);
			
			//Query columns with student id greater than 7
//			criteria.add(Restrictions.gt("studentId", 7));
			
			//Query columns with student id greater than or equal to 7
//			criteria.add(Restrictions.ge("studentId", 7));
			
			//Query columns with student id less than 7
//			criteria.add(Restrictions.lt("studentId", 7));
			
			//Query the column with student id equal to 7
//			criteria.add(Restrictions.eq("studentId", 7));
			
			//Fuzzy query addressDetail data starting with province A
//			criteria.add(Restrictions.like("addressDetail", "A province%");
			
			//Fuzzy query addressDetail data that does not start with province A
//			criteria. Add (restrictions. Like ("addressdetail", "province a%");
			
			//Query the content of student id between 6-7, 6 < = x < = 7
//			criteria.add(Restrictions.between("studentId", 6, 7));
			
			//Query whether the student id is empty
			criteria.add(Restrictions.isNull("studentId"));
			
			//Query whether addressDetail is empty
//			criteria.add(Restrictions.isNull("addressDetail"));
				
			addressList = criteria.list();
			transaction.commit();

3. Multi condition query (using deprecated method)

The old method is to do multi conditional processing

List<Address> addressList = null;
		try {
			transaction = session.beginTransaction();
			//Create Criteria according to the corresponding entity class
			Criteria criteria = session.createCriteria(Address.class);
			
			//Add conditions: one is studentId=7 and the other is studentId=8
			Criterion c1 = Restrictions.eq("studentId", 7);
			Criterion c2 = Restrictions.eq("studentId", 8);	
			
			//or is used here to combine the above conditions
			LogicalExpression or = Restrictions.or(c1,c2);
			criteria.add(or);
			
			//and is used here to combine the above conditions
//			LogicalExpression and = Restrictions.and(c1,c2);
//			criteria.add(and);
			
			addressList = criteria.list();
			transaction.commit();

4. Paging processing (using deprecated methods)

Paging processing

List<Address> addressList = null;
		try {
			transaction = session.beginTransaction();
			//Create Criteria according to the corresponding entity class
			Criteria criteria = session.createCriteria(Address.class);
			
			//Set starting data location
			criteria.setFirstResult(0);
			//Set the amount of data displayed
			criteria.setMaxResults(1);
			
			addressList = criteria.list();
			transaction.commit();

5. Sort processing (using deprecated methods)

Sorting processing

List<Address> addressList = null;
		try {
			transaction = session.beginTransaction();
			//Create Criteria according to the corresponding entity class
			Criteria criteria = session.createCriteria(Address.class);
			
			//According to the positive order of provinces
			criteria.addOrder(Order.asc("provinces"));
			//Reverse order according to studentId
			criteria.addOrder(Order.desc("studentId"));
			
			addressList = criteria.list();
			transaction.commit();

6. Deprecated method

List<Object> addressList = null;
		try {
			transaction = session.beginTransaction();
			//Create Criteria according to the corresponding entity class
			Criteria criteria = session.createCriteria(Address.class);
			
			//How many pieces of data are queried
//			criteria.setProjection(Projections.rowCount());
			
			//Get a single property: provinces
//			criteria.setProjection(Projections.property("provinces"));
			
			//Find the average number of studentId
//			criteria.setProjection(Projections.avg("studentId"));
			
			//Query the data of services after de duplication
//			criteria.setProjection(Projections.distinct(Projections.property("provinces")));
			
			//Query how many lines there are after removing duplicates in the provinces
//			criteria.setProjection(Projections.countDistinct("provinces"));
			
			//Query the maximum value of studentId
//			criteria.setProjection(Projections.max("studentId"));
			
			//Query the minimum value of studentId
//			criteria.setProjection(Projections.min("studentId"));
			
			//Query studentId summation
			criteria.setProjection(Projections.sum("studentId"));
			
			addressList = criteria.list();
			transaction.commit();

Some little knowledge

What's the difference between null and empty?

There is a judgment between null and empty. Null means nothing is null, "not null. But empty includes" and nothing.

Project address

The corresponding table creation statement is also in it.
https://gitee.com/mrchen13427566118/ssh_hibernate_learn.git
SSH inside_ hibernate_ standard_ query.
If you don't know how to open it, you can read this article https://blog.csdn.net/weixin_43987277/article/details/116936221 The third point inside

Topics: Hibernate