美文网首页
躲不过JPA的坑之级联保存

躲不过JPA的坑之级联保存

作者: 欧子有话说_ | 来源:发表于2022-09-05 09:34 被阅读0次

前言

相信大部分java小伙伴在日常开发过程中,在存储操作层都会选择更容易上手的jpa,各种封装,通过注解等方式,简化了大量的代码内容,同时提升了开发效率,但是(敲黑板,永远都躲不开的但是...)相应也将一些问题隐藏,需要更多的时间去理解、分析才能找到病根。

JPA简介

JPA全称(Java Persistence API),通常叫持久层API。它提供面向对象的编程模型,让使用者更容易理解与上手;底层也提供了通用接口,可以实现与其它框架快速集成。下面整理一些基础概念,不了解的可以参考下:

  • JPA是一种ORM规范。
  • Hibernate是JPA的一个子集。
  • Spring Data JPA,是JPA的代码实现,同时底层为Hibernate技术实现。

JPA实战-级联操作

目前小编所开发项目的数据持久层大部分采用的是Spring Data JPA,经过长时间的使用验证,发现出了级联保存问题,即调用repository的save方法后,发现数据结果却是“丢三落四”,这就很让人脑袋疼,下面结合代码一起分析下。

  • 代码场景:用户与住址是多对多的关系,JPA中描述这种关系通常使用@ManyToMany注解,首先是创建对象;

<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//维护端
public class User{
//属性代码省略
@ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
@JoinTable(name = "user_addresses",
joinColumns = {@JoinColumn(name = "user_id",referencedColumnName = "user_id")},
inverseJoinColumns = {@JoinColumn(name = "address_id",referencedColumnName = "address_id")})
private Set<Address> addresses = new HashSet<>();
}</pre>

<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//被维护方
public class Address{
//属性代码省略
@ManyToMany(mappedBy = "addresses")
@JsonIgnore
private Set<User> users = new HashSet<>();
}</pre>

  • 最初我只在维护端设置的级联属性(Cascade.ALL),然后通过维护端进行保存,很顺利,User表、Address表、user_addresses关联表都能保存,代码如下,没毛病。

<pre class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">// 维护端操作
public User save(){
Address address = new Address();
address.setAddressName("北京海淀");
User user1 = new User();
user1.setName("阿韦");
user1.getAddresses().add(address);
return UserService.save(User1);
}</pre>

  • 然后我换了个思路,从被维护端能不能进行保存,然后就立即进行尝试。代码如下:

<pre class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">// 被维护端操作
public Address save(){
Address address = new Address();
address.setAddressName("北京海淀");
User user1 = new User();
user1.setName("阿韦");
address.getUsers().add(user1);
return addressService.save(address);
}</pre>

  • 结果,我以为能够成功的时候,发现只保存了Address一张表,还没报错,就是User表和关联表死活没有数据。此时,问题已经复现,查阅各种文档说明,大概是需要添加级联属性,改造内容如下:

<pre class="prettyprint hljs sql" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER,mappedBy = "addresses")
private Set<User> users = new HashSet<>();</pre>

  • 修改之后再一次进行保存,两个实体类的表确实都生成了数据,但是关联表还是没有,这时已经快要抓狂,但问题还是没解决,继续耐心分析,很快就在控制台打印sql处发现了一些猫腻:
image.png
  • 果然是没有对关联表的数据操作,接下来又是一顿探索,终于找到根源了,由被维护端发起操作的时候,需要为关联类属性添加彼此,感觉真是关联到“恩爱”的地步了,羡煞旁人啊!!!

<pre class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">// 被维护端操作
public Address save(){
Address address = new Address();
address.setAddressName("中央花园");
User user1 = new User();
user1.setName("阿韦");
user1.setHeight("170");
user1.setSex("1");
//在彼此定义对方类集合中添加
user1.getAddresses().add(address);
address.getUsers().add(user1);
return addressService.save(address);
}</pre>

image.png
  • 最终彻底解决掉了问题,数据持久化到相应的表中。自己也确实松了一口气,过程虽然充满曲折,但结果还是非常鼓舞自己的。

心得

希望本文能够帮你避开jpa的坑,这就是小编最骄傲的事情。每一条道路上都是坑洼不平的,有些时候可能会掉入坑里,希望每个人都能重新站起来,跳过阻挡你的“坑”,最终会收获到很多,让自己变得更强。

相关文章

  • 躲不过JPA的坑之级联保存

    前言 相信大部分java小伙伴在日常开发过程中,在存储操作层都会选择更容易上手的jpa,各种封装,通过注解等方式,...

  • Jpa级联操作

    Jpa级联操作 级联web记JPA一个问题,Multiple representations of the sam...

  • jpa 级联

    jpa级联 对应项里面似乎不用再做处理了 在做级联删除的时候应该@onetomany里面的orphanRemova...

  • spring boot入门——一个事务回滚问题

    一、踩到小坑 application.yml的jpa,hibernate配置如下 一个Servicer如下 执行之...

  • Spring-Data-Jpa 保存时save和saveAndF

    Spring-Data-Jpa 保存时save和saveAndFlush的区别 Jpa是我们经常用到的ORM框架,...

  • @OneToOne 入门

    几个问题 什么是级联操作? 级联保存操作 当两边只使用@OneToOne会创建表,并且会建外键,保存会报错,说明@...

  • Hibernate 关联关系映射

    本文包括:1、一对多结构的准备2、双向关联与单向关联3、级联保存4、级联删除5、cascade 属性——级联6、i...

  • JPA 坑

    查出一个实体后,想改变对象中的属性时,用Set 操作。若下方有其它对象的save操作时,就同时把查出的实体做了更改...

  • 2018-03-16

    聊聊JPA Criteria查询中的坑 JPA Criteria查询被称作动态安全类型查询,比JPQL这种方式更加...

  • 得此“牛肉”,别无他求~

    香不过肉桂,醇不过水仙。是每一个热衷岩茶之人都躲不过的诱惑。“牛肉”,爱茶者对武夷岩茶“三坑两涧”核心产区中牛栏坑...

网友评论

      本文标题:躲不过JPA的坑之级联保存

      本文链接:https://www.haomeiwen.com/subject/zvuvnrtx.html