Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 385|回复: 0

SpringData JPA框架使用时出现JSON循环依赖解决方案

[复制链接]
  • TA的每日心情
    奋斗
    3 天前
  • 签到天数: 790 天

    [LV.10]以坛为家III

    2049

    主题

    2107

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    722766
    发表于 2021-6-19 01:06:06 | 显示全部楼层 |阅读模式

    困扰许久的问题终于解决了,之前项目太赶,没有深入学习解决,不甘心,今天再次搭起架子试试,哈哈,终于解决!

       
      @ManyToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH},optional=false)
        @JoinColumn(name="approvalForCarId",nullable = false)
        private ApprovalForCar approvalForCar;
    
    
    
    
       @JsonIgnoreProperties(ignoreUnknown = true, value = {"approvalForCar"})
        @OneToMany(mappedBy = "approvalForCar",cascade = { CascadeType.MERGE,CascadeType.REMOVE},fetch = FetchType.EAGER)
        private List<DistributeCar> distributeCars = new ArrayList<>(0);

     

      双向关联时json序列化时会出现死循环的情况

    jackson解决问题的方式是循环的那一部分不解析
    JsonIgnoreProperties配置不解析的属性

     

    g:第一个“栗子",只有两个类关联,但互相引用了,多对多和一对一这里都适用
    Book类上面放入

    @JsonIgnoreProperties(ignoreUnknown = true, value =
    {"hibernateLazyInitializer", "handler", "fieldHandler"})
    public class Book{
    ... ...

    Book类中属性上注解,此属性Author中引用了private Set books;

    public class Book{
    ... ...
    @JsonIgnoreProperties(ignoreUnknown = true, value = {"books"})
    private Set<Author> authors;
    ... ...

    Author类上面放入

    @JsonIgnoreProperties(ignoreUnknown = true, value =
    {"hibernateLazyInitializer", "handler", "fieldHandler"})
    public class Author{
    ... ...

    Author类中属性上注解,此属性Book中引用了private Set authors;

    public class Author{
    ... ...
    @JsonIgnoreProperties(ignoreUnknown = true, value = {"authors"})
    private Set<Book> books;
    ... ...

    上面的例子“栗子"过去久远,只对关键需要配置的地方进行了标注
    eg:第二个“栗子",有三个类关联,通过一个中间类关联直接贴代码

    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

    import javax.persistence.*;
    import java.io.Serializable;
    import java.util.HashSet;
    import java.util.Set;

    @Entity
    @JsonIgnoreProperties(ignoreUnknown = true, value =
    {"hibernateLazyInitializer", "handler", "fieldHandler"})
    public class Book implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @TableGenerator(name = "book_generator", table = "tables_sequence", allocationSize = 1, initialValue = 0)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "book_generator")
    private int id;
    private String name;

    @OneToMany(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
    @JsonIgnoreProperties(ignoreUnknown = true, value = {"book"})
    private Set<BookPublisher> bookPublishers;

    public Book() {
    }

    public Book(String name) {
    this.name = name;
    bookPublishers = new HashSet<>();
    }

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public Set<BookPublisher> getBookPublishers() {
    return bookPublishers;
    }

    public void setBookPublishers(Set<BookPublisher> bookPublishers) {
    this.bookPublishers = bookPublishers;
    }
    }

    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

    import javax.persistence.*;
    import java.io.Serializable;
    import java.util.Set;

    @Entity
    @JsonIgnoreProperties(ignoreUnknown = true, value =
    {"hibernateLazyInitializer", "handler", "fieldHandler"})
    public class Publisher implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @TableGenerator(name = "pub_generator", table = "tables_sequence", allocationSize = 1, initialValue = 0)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "pub_generator")
    private int id;
    private String name;
    @OneToMany(mappedBy = "publisher")
    @JsonIgnoreProperties(ignoreUnknown = true, value = {"publisher"})
    private Set<BookPublisher> bookPublishers;

    public Publisher() {

    }

    public Publisher(String name) {
    this.name = name;
    }

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public Set<BookPublisher> getBookPublishers() {
    return bookPublishers;
    }

    public void setBookPublishers(Set<BookPublisher> bookPublishers) {
    this.bookPublishers = bookPublishers;
    }
    }

    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

    import javax.persistence.*;
    import java.io.Serializable;
    import java.util.Date;

    @Entity
    @Table(name = "book_publisher")
    @JsonIgnoreProperties(ignoreUnknown = true, value =
    {"hibernateLazyInitializer", "handler", "fieldHandler"})
    public class BookPublisher implements Serializable {

    @Id
    @ManyToOne
    @JoinColumn(name = "book_id")
    @JsonIgnoreProperties(ignoreUnknown = true, value = {"bookPublishers"})
    private Book book;

    @Id
    @ManyToOne
    @JoinColumn(name = "publisher_id")
    @JsonIgnoreProperties(ignoreUnknown = true, value = {"bookPublishers"})
    private Publisher publisher;

    @Column(name = "published_date")
    private Date publishedDate;

    public Book getBook() {
    return book;
    }

    public void setBook(Book book) {
    this.book = book;
    }


    public Publisher getPublisher() {
    return publisher;
    }

    public void setPublisher(Publisher publisher) {
    this.publisher = publisher;
    }

    public Date getPublishedDate() {
    return publishedDate;
    }

    public void setPublishedDate(Date publishedDate) {
    this.publishedDate = publishedDate;
    }
    }


    总结:
    本实体中引入了另外一个实体,但另外一个实体也引用了自己无论是集合还是单个实体。jakson在格式化数据的时候会动态过滤掉此属性中对本身对象的引用。

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-9-13 16:52 , Processed in 2.120415 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表