There are ten million road, but the most important thing is to find your suitable, there are thousands of things, only done, can we truly know you like it or not, there’s a phrase in the “run” like a dog, of the unknown road lose courage, true tomb of youth, life is always want to do something never done, or talk about how to love, this blog is my one hundredth blog, In a sense, it is of great significance. Learning is an input-output process, and blogging is of great help to knowledge learning. It can help me to truly understand technical points and systematically summarize the knowledge and technology stack.

This time, Spring Data Jpa

This framework as a part of the Spring system, more and more people are known, the market is more use of Mybatis, but Spring Data JPA also has more advantages.

First, we have used some database query framework Hibernate, and Spring Data Jpa is associated with some, but many people including me before the understanding of the association between them is problematic

Hibernate is a framework that uses the Jpa specification. Note that Jpa is not Spring Data Jpa, but a specification. The purpose of Jpa specification is to achieve uniformity, otherwise different frameworks using different specifications will be confusing. Spring Data Jpa also uses the Jpa specification. The difference between Heibernate and Spring Data Jpa is that it makes a layer of abstraction on the basis of Jpa specification, while Hibernate does not, but many people online say that Spring Data Jpa is based on Hibernate. I think a big part of the problem is that it is really similar to Hbernate, orM mapping, HQL statements, etc. Some people say that JPA is provided by Hibernate, but I think since it is a general specification, it should be provided by more than Hibernate.

So why choose SpringDataJpa, first of all, it is very convenient to write query, through the method name can be implemented through the way of interface, the following talk about his several interfaces

public interface Repository<T, ID extends Serializable> {

}
Copy the code

This is a core interface, it doesn’t do anything, but it doesn’t seem to do anything, it’s a way of coding abstract ideas, it’s a tag tag class and id type, and it turns out in Spring’s dynamic proxy that it’s a subclass of database operations.

I can then view his Hierachy through the Hierachy of idea ->type to see what implementations he has. This step is very important because the principles of learning the framework are dependent on reading the source code, and there are ways to learn the source code. Before learning Spring source code is because of the structure of the unclear lead to slow learning progress, so still need some skills.

We see that he has several subclasses, and his subclasses are interesting, a CrudRestitory, and pagingsortingitory, and JpaRestitory, and each of these subclasses has something they can do, and they’re inheritance.

CrudRestitory, such as naming, adding, deleting, and deleting, and then paging and sortingitory add paging sort on top of that, and JPare Tory add batch on top of that.

So I read the book Spring Data Jpa from Getting Started to Mastering. It’s very detailed. They have an implementation class

public class SimpleJpaRepository<T, ID extends Serializable> implements JpaRepository<T, ID>, JpaSpecificationExecutor<T> { private final JpaEntityInformation<T, ? > entityInformation; private final EntityManager em; private final PersistenceProvider provider; private CrudMethodMetadata crudMethodMetadata;Copy the code

That’s it. Then we see the EntityManager class, which springDataJpa uses to manage sessions, just like Mybatis SqlSessionFactory, which received this first.

Spring’s JPA implementation has two methods for saving data. One is sava and the other is saveAndFlush

Spring’s JPA commits database to saveAndFlush by using save. Instead of committing all at once, spring’s JPA commits database to memory first and then commits. In addition, loading into memory and committing together can also reduce the pressure on the database, and there will be optimization, such as the order adjustment of insert delete UPDATE, but this may have problems, for example, in a transaction, the later query depends on the previous update operation, so if the database cannot be submitted in time, This will cause problems with the query. In this case, you should use saveAndFlush. In fact, there is a lot of KNOWLEDGE about Jpa. I will write about it today and continue to update it.