Preface
Basic introduction to Spring-data-jpa: The reason for the birth of JPA is to integrate third-party ORM frameworks and establish a standard method. Baidu Encyclopedia says that JDK is developing in this direction in order to achieve the unity of ORM. However, it has not been fully implemented. In the ORM framework, Hibernate is a large army, widely used, convenient, and has strong capabilities. At the same time, Hibernate is also well integrated with JPA. We can think that JPA is the standard, and in fact, JPA is almost all interfaces, and implementations are all done by Hibernate. From a macro perspective, Hibernate operates very well under the unification of JPA.
Recently, I have been using Springboot and Spring data jpa. Using jpa can make me more convenient to operate the database, but I have encountered many pitfalls during use. I will record the following article. I won’t say much below. Let’s take a look at the detailed introduction together.
Scene:
Dynamic query, paging query, query different data tables according to different states passed in, and use map to convert VO before passing in the page object. The different use places of pageable affects the correctness of the paged data, so we will discuss it.
premise:
The Page object is enclosed in VO, and the return data includes paged data
@ApiModelProperty("Record")private Page<ActivityRecordVO> activityRecordVOList;@ApiModelProperty("Quantity")private Integer num = 0;@ApiModelProperty("Amount")private BigDecimal totalMoney = BigDecimal.valueOf(0);Incorrect application:
List<ActivityRecordVO> activityRecordVOList = new ArrayList<>(); if (receiveSendRecordRequestVO.getSendOrReceiveType() == SendOrReceiveType.RECEIVE) { List<ChallengeRecord> challengeRecordList = challengeRecordDao.findByUserIdAndDeleteType(userId, DeleteType.FALSE); if (!CollectionUtils.isEmpty(challengeRecordList)) { activityRecordVOList = challengeRecordList.stream() .map(this::challengeRecordToActivityRecordVO) .collect(Collectors.toList()); } } else if (receiveSendRecordRequestVO.getSendOrReceiveType() == SendOrReceiveType.SEND) { List<Activity> activityList = activityDao.findByUserIdAndDeleteType(userId, DeleteType.FALSE); if (!CollectionUtils.isEmpty(activityList)) { activityRecordVOList = activityList.stream() .map(this::activityTOActivityRecordVO) .collect(Collectors.toList()); } }activityReceiveSendRecordVO.setActivityRecordVOList(new PageImpl<>(activityRecordVOList, pageable, activityRecordVOList.size()));
Analysis: The passed pageable only uses new PageIml to convert List into page object when set to VO. Although the total number of pages and total number of lines reported in the front-end is correct, the number of lines on the first page is all, and the data is abnormal!
Correct reference method:
Use Specifications to dynamically query and map out the corresponding paging object according to the query conditions (this block code varies according to the requirements). At this time, the pageable passed in findAll is effective, and the correct paging information will be displayed.
Code block reference:
xxxCommonSpecUtil is a self-proclaimed specification tool class, similar to the native spring data jpa native query method.
Page<ActivityRecordVO> page = new PageImpl<>(activityRecordVOList, pageable, activityRecordVOList.size()); if (receiveSendRecordRequestVO.getSendOrReceiveType() == SendOrReceiveType.RECEIVE) { Specifications<ChallengeRecord> spec = Specifications.where( challengeCommonSpecUtil.equal("userId", userId)) .and(challengeCommonSpecUtil.equal("deleteType", DeleteType.FALSE)); page = challengeRecordDao.findAll(spec, pageable).map(this::challengeRecordToActivityRecordVO); } else if (receiveSendRecordRequestVO.getSendOrReceiveType() == SendOrReceiveType.SEND) { Specifications<Activity> spec = Specifications.where( activityCommonSpecUtil.equal("userId", userId)) .and(activityCommonSpecUtil.equal("deleteType", DeleteType.FALSE)); page = activityDao.findAll(spec, pageable).map(this::activityTOActivityRecordVO); }Note: activityReceiveSendRecordVO is encapsulated VO, which contains the returned Page object
activityReceiveSendRecordVO.setActivityRecordVOList(page);
Summarize
After using spring data jpa for so long, I think Specifications is very useful and not prone to errors. It is also my favorite encoding style. However, I usually use the simple and crude method of new PageImpl<>() to query data and associate too many tables. I will return directly at the end. The deeper level needs to be discussed again!
Okay, the above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.