Summary of pit records using spring data JPA

preface

Basic introduction to spring data JPA: JPA was born to integrate the third-party ORM framework and establish a standard way. Baidu Encyclopedia said that JDK is developing in this direction in order to realize the unification of ORM, but it has not been fully realized. In the ORM framework, hibernate is a large force, which is widely used, convenient and capable. At the same time, hibernate is well integrated with JPA. We can think that JPA is a standard. In fact, JPA is almost an interface, and the implementation is done by hibernate. From a macro point of view, hibernate runs well under the unification of JPA.

Recently, I have been using springboot and spring data JPA. Using JPA can make it easier for me to operate the database, but I have also encountered many pitfalls in use. The following article will record it. I won't say more below. Let's take a look at the detailed introduction together.

Scenario:

Dynamic query, paging query, query different data tables according to different incoming States, and use map to convert VO before passing in page objects. The use of pageable in different places affects the correctness of paging data.

Premise:

The page object is enclosed in the VO, and the returned data includes the paging data

Wrong application:

Analysis: the pageable passed in only uses new pageiml to convert the list into a page object when it is set into vo. Although the total number of pages and articles in the front-end report are correct, the number of articles in the first page is all, and the data is abnormal!

Correct reference practice:

Using specifications, first dynamically query and map the corresponding paging objects according to the query criteria (this block of code varies according to requirements). At this time, the pageable passed in by findall is effective, and the correct paging information will be displayed.

Code block reference:

Xxxcommonspecutil is a self styled specification tool class, which is similar to the native spring data JPA query method.

Note: activityreceivesendrecordvo is an encapsulated VO, including the returned page object

summary

After using spring data JPA for such a long time, I think specifications is very easy to use and not easy to make mistakes. It is also my favorite coding style. However, new pageimpl < > () this simple and crude method is generally used to query too many tables associated with data. In the end, it is returned directly. It needs to be discussed further!

Well, the above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>