springboot整合jpa踩过的坑(二)

一、org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion(StackOverflowError);nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain:xxxxxx

从异常中信息能够看出在转化为json的过程中产生了死循环,把A类转化为json的时候,A类中做了B类的关联映射,而B类中又管理了A类,这样就产生了json转化死循环,究其原因是因为在双向关联的时候没有指定哪一方来维护关联关系,所以才会产生这样的原因,有两种解决方法:

①在一方的的get方法中加上@JsonBackReference注解

②指定维护关系,

(1)可以在 one 方指定 @OneToMany 注释并设置 mappedBy 属性,以指定它是这一关联中的被维护端,many 为维护端。

例:@OneToMany(fetch = FetchType.LAZY ,targetEntity = DeviceEntity.class,mappedBy = "areaId")

(2)在双向的一对一关联中,需要在关系被维护端(inverse side)中的 @OneToOne 注释中指定 mappedBy,以指定是这一关联中的被维护端

TemplateEntity.java

@OneToOne(fetch = FetchType.LAZY ,cascade = CascadeType.ALL, mappedBy = "templateEntity")
    public ConfigEntity getConfigEntity() {
        return configEntity;

    }

ConfigEntity.java

@JoinColumn(name = "template_id" , insertable = false , updatable = false)
    public TemplateEntity getTemplateEntity() {
        return templateEntity;
    }

(mappedBy属性定义了此类为双向关系的维护端,注意:mappedBy 属性的值为此关系的另一端的属性名。)

二、org.hibernate.MappingException: Could not determine type for: xxxxxx

这是因为在实体类中建立关联关系的时候,要把关联关系注解加在getter方法,在属性或setter方法上就会出现这样情况

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要将Spring Boot与JPA整合,首先需要在pom.xml文件中添加相应的依赖。这些依赖包括Spring Data JPA和MySQL驱动。例如,在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> ``` 接下来,你需要在Spring Boot应用程序的配置文件中添加数据库连接配置。你可以在application.properties或application.yml文件中添加以下配置: ```yaml spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name spring.datasource.username=your_username spring.datasource.password=your_password spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update ``` 在这个配置中,你需要将`your_database_name`替换为你的数据库名称,`your_username`替换为你的数据库用户名,`your_password`替换为你的数据库密码。 然后,你可以创建实体类和存储库接口。实体类用于映射数据库表,而存储库接口用于定义对数据库的操作。可以使用`@Entity`和`@Table`注解来标识实体类,使用`@Repository`或`@RepositoryRestResource`注解来标识存储库接口。 最后,你可以在Spring Boot应用程序的启动类上添加`@EnableJpaRepositories`注解,以启用JPA存储库。 以上是将Spring Boot与JPA整合的基本步骤。通过这种方式,你可以使用JPA进行数据库操作,并且Spring Boot会自动为你处理大部分的配置和实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [springboot整合jpa,步骤详细(图文结合讲解)](https://blog.csdn.net/weixin_43442127/article/details/119953836)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [springboot整合JPA](https://blog.csdn.net/weixin_44740485/article/details/124904112)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

索码理

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值