记: 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.

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

相关文章
|
7天前
|
存储 NoSQL Java
使用Java和Spring Data构建数据访问层
本文介绍了如何使用 Java 和 Spring Data 构建数据访问层的完整过程。通过创建实体类、存储库接口、服务类和控制器类,实现了对数据库的基本操作。这种方法不仅简化了数据访问层的开发,还提高了代码的可维护性和可读性。通过合理使用 Spring Data 提供的功能,可以大幅提升开发效率。
53 21
|
2月前
|
SQL Java 数据库连接
spring和Mybatis的各种查询
Spring 和 MyBatis 的结合使得数据访问层的开发变得更加简洁和高效。通过以上各种查询操作的详细讲解,我们可以看到 MyBatis 在处理简单查询、条件查询、分页查询、联合查询和动态 SQL 查询方面的强大功能。熟练掌握这些操作,可以极大提升开发效率和代码质量。
139 3
|
4月前
|
存储 Java API
如何使用 Java 记录简化 Spring Data 中的数据实体
如何使用 Java 记录简化 Spring Data 中的数据实体
56 9
|
4月前
|
SQL Java 关系型数据库
Springboot引入jpa来管理数据库
Springboot引入jpa来管理数据库
89 0
Springboot引入jpa来管理数据库
|
4月前
|
SQL Java 数据库连接
springBoot+Jpa(hibernate)数据库基本操作
springBoot+Jpa(hibernate)数据库基本操作
101 0
|
5月前
|
Java 数据库连接 API
【Java笔记+踩坑】Spring Data JPA
从常用注解、实体类和各层编写方法入手,详细介绍JPA框架在增删改查等方面的基本用法,以及填充用户名日期、分页查询等高级用法。
|
6月前
|
Java Spring 数据库
怎样动动手指就能实现数据操作?Spring Data JPA背后的魔法揭秘
【8月更文挑战第31天】在Java开发中,数据库交互至关重要。传统的JDBC操作繁琐且难维护,而Spring Data JPA作为集成JPA的数据访问层解决方案,提供了CRUD等通用操作接口,显著减少代码量。通过继承`JpaRepository`,开发者能轻松实现数据的增删改查,甚至复杂查询和分页也不再困难。本文将通过示例详细介绍如何利用Spring Data JPA简化数据访问层的开发,提升代码质量和可维护性。
58 0
|
6月前
|
存储 Java 数据库
|
6月前
|
存储 Java API
|
Java Spring Kotlin
第2讲 Kotlin + Spring Boot 集成 JPA 【Kotlin + Spring Boot 服务端开发课程系列】
【Kotlin + Spring Boot 服务端开发课程系列】第2讲 Kotlin + Spring Boot 集成 JPA 【视频教程】 投影片01.
1085 0