From 29653322f755659d8aeeca404dd84739b085164a Mon Sep 17 00:00:00 2001 From: Janis Blatsios Date: Fri, 7 Jun 2024 09:36:22 +0200 Subject: [PATCH] json managed references --- src/main/java/pl/janis/komornik/entities/Category.java | 2 ++ src/main/java/pl/janis/komornik/entities/Debt.java | 4 ++-- src/main/java/pl/janis/komornik/entities/Expense.java | 7 ++++--- src/main/java/pl/janis/komornik/entities/Group.java | 2 ++ src/main/java/pl/janis/komornik/entities/User.java | 8 ++++---- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main/java/pl/janis/komornik/entities/Category.java b/src/main/java/pl/janis/komornik/entities/Category.java index 2f1c844..7991fc6 100644 --- a/src/main/java/pl/janis/komornik/entities/Category.java +++ b/src/main/java/pl/janis/komornik/entities/Category.java @@ -1,5 +1,6 @@ package pl.janis.komornik.entities; +import com.fasterxml.jackson.annotation.JsonManagedReference; import jakarta.persistence.*; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -31,6 +32,7 @@ public class Category { private String categoryIconName; @OneToMany(mappedBy = "category") + @JsonManagedReference @ToString.Exclude private Set expenses = new LinkedHashSet<>(); diff --git a/src/main/java/pl/janis/komornik/entities/Debt.java b/src/main/java/pl/janis/komornik/entities/Debt.java index a49276e..d3822fc 100644 --- a/src/main/java/pl/janis/komornik/entities/Debt.java +++ b/src/main/java/pl/janis/komornik/entities/Debt.java @@ -1,6 +1,6 @@ package pl.janis.komornik.entities; -import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonBackReference; import jakarta.persistence.*; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -26,7 +26,7 @@ public class Debt { @ManyToOne(fetch = FetchType.LAZY, optional = false, cascade = CascadeType.MERGE) @JoinColumn(name = "expense_id") - @JsonIgnore + @JsonBackReference @ToString.Exclude private Expense expense; diff --git a/src/main/java/pl/janis/komornik/entities/Expense.java b/src/main/java/pl/janis/komornik/entities/Expense.java index 2deb281..97814d4 100644 --- a/src/main/java/pl/janis/komornik/entities/Expense.java +++ b/src/main/java/pl/janis/komornik/entities/Expense.java @@ -1,7 +1,7 @@ package pl.janis.komornik.entities; +import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonFormat; -import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -42,6 +42,7 @@ public class Expense { @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE}) @JoinColumn(name = "category_id") + @JsonBackReference private Category category; @Column(name = "currency", nullable = false) @@ -54,12 +55,12 @@ public class Expense { private BigDecimal amount; @OneToMany(fetch = FetchType.EAGER, mappedBy = "expense", cascade = CascadeType.ALL, orphanRemoval = true) - @ToString.Exclude + @JsonBackReference private List debt; @ManyToOne(fetch = FetchType.LAZY, optional = false, cascade = {CascadeType.MERGE}) @JoinColumn(name = "group_id", nullable = false) - @JsonIgnore + @JsonBackReference @ToString.Exclude private Group group; diff --git a/src/main/java/pl/janis/komornik/entities/Group.java b/src/main/java/pl/janis/komornik/entities/Group.java index f970663..910a43e 100644 --- a/src/main/java/pl/janis/komornik/entities/Group.java +++ b/src/main/java/pl/janis/komornik/entities/Group.java @@ -1,5 +1,6 @@ package pl.janis.komornik.entities; +import com.fasterxml.jackson.annotation.JsonManagedReference; import jakarta.persistence.*; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -37,6 +38,7 @@ public class Group { private String groupIconName; @OneToMany(mappedBy = "group") + @JsonManagedReference @ToString.Exclude private List expenses; diff --git a/src/main/java/pl/janis/komornik/entities/User.java b/src/main/java/pl/janis/komornik/entities/User.java index 7cb99ee..f5a0a0f 100644 --- a/src/main/java/pl/janis/komornik/entities/User.java +++ b/src/main/java/pl/janis/komornik/entities/User.java @@ -1,6 +1,6 @@ package pl.janis.komornik.entities; -import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonManagedReference; import jakarta.persistence.*; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -44,17 +44,17 @@ public class User implements UserDetails { private Boolean isVerified; @ManyToMany(mappedBy = "users") - @JsonIgnore + @JsonManagedReference @ToString.Exclude private List groups; @OneToMany(mappedBy = "userFrom", cascade = CascadeType.ALL) - @JsonIgnore + @JsonManagedReference @ToString.Exclude private List debtFrom; @OneToMany(mappedBy = "userTo", cascade = CascadeType.ALL) - @JsonIgnore + @JsonManagedReference @ToString.Exclude private List debtTo;