记: Spring Data Jpa @OneToMany 级联查询被动触发的问题
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
记: Spring Data Jpa @OneToMany 级联查询被动触发的问题的更多相关文章
- 【Spring Data 系列学习】Spring Data JPA @Query 注解查询
[Spring Data 系列学习]Spring Data JPA @Query 注解查询 前面的章节讲述了 Spring Data Jpa 通过声明式对数据库进行操作,上手速度快简单易操作.但同时 ...
- spring data jpa 一对多查询
在一对多关系中,我们习惯把一的一方称之为主表,把多的一方称之为从表.在数据库中建立一对多的关系,需要使用数据库的外键约束. 什么是外键? 指的是从表中有一列,取值参照主表的主键,这一列就是外键. pa ...
- spring data jpa 多对多查询
package com.ytkj.dao; import com.ytkj.entity.Customer; import com.ytkj.entity.Role; import org.sprin ...
- Spring data JPA 理解(默认查询 自定义查询 分页查询)及no session 三种处理方法
简介:Spring Data JPA 其实就是JDK方式(还有一种cglib的方式需要Class)的动态代理 (需要一个接口 有一大堆接口最上边的是Repository接口来自org.springfr ...
- Spring data jpa 复杂动态查询方式总结
一.Spring data jpa 简介 首先我并不推荐使用jpa作为ORM框架,毕竟对于负责查询的时候还是不太灵活,还是建议使用mybatis,自己写sql比较好.但是如果公司用这个就没办法了,可以 ...
- Spring Data JPA应用 之查询分析
在Spring Data JPA应用之常规CRUD操作初体验 - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)尾附上了JpaRepository接口继承关系及方法,可以知道JpaRepos ...
- Spring Data Jpa (三)定义查询方法
本章详细讲解如何利用方法名定义查询方法(Defining Query Methods) (1)定义查询方法的配置方法 由于Spring JPA Repository的实现原理是采用动态代理的机制,所以 ...
- spring data jpa Specification动态查询
package com.ytkj.entity; import javax.persistence.*; import java.io.Serializable; /** * @Entity * 作用 ...
- 关于Spring Data JPA 多表查询 返回自定义Vo的问题记录
这两天开了一个新项目,使用SpringBoot+SpringData, 刚做了一个小功能,都是一张表的操作没什么问题,今天设计到了两张表联查,两张表各取了几个字段,组合成了一个vo, 当我用原生sq ...
随机推荐
- getopt、getopt_long和getopt_long_only解析命令行参数
一:posix约定: 下面是POSIX标准中关于程序名.参数的约定: 程序名不宜少于2个字符且不多于9个字符: 程序名应只包含小写字母和阿拉伯数字: 选项名应该是单字符或单数字,且以短横 '-' 为前 ...
- array_map 用法
array_map - 将回调函数作用到数组中的每一个元素上 function add2($value) { return $value + 2; } $arr = array(1, 2, 3, 4, ...
- git 生成秘钥连接远程仓库
二.打开GitBash ,用cd命令进入本地项目目,然后把初始化一下,把本地的目录变成git本地仓库, git status 可以查看本地目录的状态信息 git init git status 三.将 ...
- supersockets多个 listener
你可以增加一个子节点 "listeners" 用于添加多对监听 ip/port: <superSocket> <servers> <server na ...
- git pull 和git fetch区别?
git:从远程分支获取最新的版本至本地有两个命令. git fetch 相当于从远程获取最新的版本至本地,但不会自动merge git pull 相当于从远程获取最新的版本并merge至本地
- 全面解读Python Web开发框架Django,利用Django构建web应用及其部署
全面解读Python Web开发框架Django Django是一个开源的Web应用框架,由Python写成.采用MVC的软件设计模式,主要目标是使得开发复杂的.数据库驱动的网站变得简单.Django ...
- 移动端Chrome Inspect调试 (Android通过Chrome Inspect调试WebView的H5)(ios手机safari,chrome调试 windows)(如果inspect的时候,是空白)
ios +chrome调试 引用https://segmentfault.com/a/1190000015428430 iTunes ios-webkit-debug-proxy-1.8-win64- ...
- TabHost选项卡的实现(二):使用Fragment实现
在上一篇博客<TabHost选项卡的实现(一):使用TabActivity实现>中,讲解了如何使用TabActivity创建管理选项卡,但是,通过TabActivity创建选项卡的方式已经 ...
- 浅谈Transformer 及Attention网络
1 Transformer 模型结构处理自然语言序列的模型有 rnn, cnn(textcnn),但是现在介绍一种新的模型,transformer.与RNN不同的是,Transformer直接把一句话 ...
- 2001年NOIP普及组复赛题解
题目涉及算法: 数的计算:动态规划: 最大公约数和最小公倍数问题:质因数分解: 求先序排列:递归: 装箱问题:动态规划(纯0-1背包问题) 数的计算 题目链接:https://www.luogu.or ...