SpringData JPA框架使用時出現JSON循環依賴解決方案


困擾許久的問題終於解決了,之前項目太趕,沒有深入學習解決,不甘心,今天再次搭起架子試試,哈哈,終於解決!

   
  @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在格式化數據的時候會動態過濾掉此屬性中對本身對象的引用。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM