I have encountered a bug in using Spring Data Jpa. Specifically,when @OneToMany was used to maintain a one-to-many relationship, lazy loading was effective.However,it may passively trigger the cascading query without using the cascading property.

My development environment :

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

**My User.class is as follows **

**My Paper.class is as follows **

My PaperService.class is as follows

My UserController.class is as follows

I would like to use Jpa's @OneToMany default lazy loading mechanism when pagination queries was produced. Additionally, I don't need the collection of papers fields associated with the user.Nevertheless,I find that the papers attribute in the user is still populated with data in the returned results.

Therefore, I conducted the following debugging:**

send a request

Observe the execution of the code:

As you can see, although the lazy loading of Jpa was normal, I found the papers values that needed to be populated by cascading queries in the response data

I guess the user's papers field in Page must have been used by the framework before the data was written back to the browser, so I started with the UserController and continued to trace the source code

Then I was found the following call: Jackson called paper's getter through reflection while serializing the data . in the package com. Fasterxml. Jackson. Databind. Ser.

**That's why I get a response in which the paper property is populated with a value, right **

**Look at it this way, even though lazy loading of Jpa is in effect, cascading queries are triggered passively **

**Lazy loading is in effect, but the cascading query is still triggered passively, which is not the result we want, I wonder what you think about it **

  • solution 1:
    @RequestMapping(path = "/getUserByPage")
public Page getUserByPage(@RequestParam Integer from, @RequestParam Integer limit, @RequestParam(required = false) String name) {
Page<User> page = userService.getUserByPage(from, limit, name);
page.getContent().forEach(user->{
user.setPapers(null);
});
return page;
}
  • solution 2: @JsonIgnore

**@JsonIgnore can handle this pretty well, so why not let @OneToMany have it? **

    @JsonIgnore
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL,fetch = FetchType.LAZY)
private Set<Paper> papers = new HashSet<>();
  • solution 3:

We can get rid of the getters (papers), but if we do that, we can't use the property by ourselves

最新文章

  1. Android 生成LayoutInflater的三种方式
  2. Win10 UI介绍之Titlebar
  3. java.lang.instrument使用
  4. try catch finally return之间的关系
  5. Python生成8位随机密码
  6. eclipse 安装scons
  7. Linux时间函数
  8. 大象的崛起!Hadoop七年发展风雨录
  9. HDOJ -- 1015
  10. sql server 数据库附加时程序集错误
  11. 基于visual Studio2013解决面试题之0301累加
  12. Unity依赖注入
  13. PHP实现开心消消乐的算法示例
  14. 《通过C#学Proto.Actor模型》之 HelloWorld
  15. Vue 知识整理—02-起步
  16. echarts立体效果地图-自定义区域及文字
  17. vue的 v-for 循环中图片加载路径问题
  18. iOS:基于RTMP的视频推流
  19. Java之事务的基本应用
  20. Java 基础 面向对象和抽象类

热门文章

  1. 学习C#泛型
  2. 4 文件操作 支持图片 视频 mp3 文本等
  3. python selenium 测试 LOG
  4. STS Eclipse IDEA 指定启动JDK版本
  5. docker + jenkins 自动化部署
  6. javascript 宽度和高度
  7. H3C PAP验证配置示例
  8. 蝶式套利(butterfly spread)
  9. 高可用之keepalived的配置文件详解
  10. 应用九:Vue之国际化(vue-i18n)