Exploring the principle of spring-data-jpa (4) - Overview of the JpaQueryExecution class

The fourth part of the spring-data-jpa principle, let's talk about JpaQueryExecution related classes. First, the class diagram and inheritance relationship

are given: JpaQueryExecution is clearly stated in spring-data-jpa:
a collection of classes that contain multiple query execution strategies. Can be executed in various flavors depending on the return type in most org.springframework.data.repository.query.QueryMethod and AbstractStringBasedJpaQuery.

We see that spring-data-jpa uses an inner class to implement a subclass of JpaQueryExecution, which is a (sub)class that contains multiple query execution strategies. This is actually a relatively closed form, so that users can only instantiate the eight subclasses given by spring-data-jpa. You cannot implement your own subclass of JpaQueryExecution and load it through configuration when the spring framework is initialized.

In fact, the role of JpaQueryExecution is to judge the class instance of the class variable JpaQueryMethod method, and select a subclass of JpaQueryExecution for instantiation through the protected JpaQueryExecution getExecution() method of the AbstractJpaQuery class.
The following is the getExecution() method code of the AbstractJpaQuery abstract class:
	protected JpaQueryExecution getExecution() {

		if (method.isStreamQuery()) {
			return new StreamExecution();
		} else if (method.isProcedureQuery()) {
			return new ProcedureExecution();
		} else if (method.isCollectionQuery()) {
			return new CollectionExecution();
		} else if (method.isSliceQuery()) {
			return new SlicedExecution(method.getParameters());
		} else if (method.isPageQuery()) {
			return new PagedExecution(method.getParameters());
		} else if (method.isModifyingQuery()) {
			return method.getClearAutomatically() ? new ModifyingExecution(method, em) : new ModifyingExecution(method, null);
		} else {
			return new SingleEntityExecution();
		}
	}

The code is very simple and clear at a glance. According to the query setting method when the method variable is instantiated, instantiate different JpaQueryExecution subclass instances to run. If it is difficult to judge, finally enter the else judgment statement to generate a SingleEntityExecution instance.

SingleEntityExecution is one of the 8 internal subclasses of JpaQueryExecution, all of which are as follows:
static class SingleEntityExecution extends JpaQueryExecution {
	@Override
	protected Object doExecute(AbstractJpaQuery query, Object[] values) {
		return query.createQuery(values).getSingleResult();
	}
}

SingleEntityExecution only covers one parent class method, and there is only one statement in the implementation query.createQuery(values).getSingleResult(); When this statement is executed, the underlying database access provider will be called, and spring-data-jpa selects hibernate by default , after performing the database access operation, return the result to this JpaQueryExecution instance.

To be continued.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326752759&siteId=291194637