springboot+jpa实现主从表的增加和查询

本文介绍了在SpringBoot 2.4与JPA环境中,如何处理一对多主从表的双向绑定,避免因深度遍历导致的`java.lang.StackOverflowError`。通过分析实体类`Letter`和`LetterCity`,指出在从表中使用`@ManyToOne`可能导致的递归调用问题,并提出解决方案:修改从表的getter方法,仅返回主表ID,以保持数据关联并防止无限递归。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

介绍springboot+jpa实现主从表的增加和查询

环境配置:springboot2.4,jpa
要求:实现一对多表的双向绑定,前后端分离
这里记录一下写的代码,这里经常由于一对多表的双向绑定关系造成反复互相调用,出现Cannot call sendError() after the response has been committed和java.lang.StackOverflowError: null。
这里附上一个解决方案,我是根据这篇博客解决了自己的问题:
SpringJpa中ManyToMany、ManyToOne导致的死循环(java.lang.StackOverflowError)两种解决办法

一.项目为maven项目,整合了spring boot和jpa,下面贴出pom.xml文件。

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true<</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值