0

I got two classes:

package com.educationalcenter.demo.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.*;
import lombok.experimental.FieldDefaults;

import javax.persistence.*;
import java.util.List;

@NoArgsConstructor
@AllArgsConstructor
@Builder
@Setter
@Getter
@FieldDefaults(level = AccessLevel.PRIVATE)
@Entity
@Table(name = "bankworker")
public class Bankworker {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long bankworkerId;
    @Column(nullable = false)
    String Fname;
    @Column(nullable = false)
    String Lname;
    @Column(nullable = false, unique = true)
    String Login;
    @Column(nullable = false)
    String Password;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_id", referencedColumnName = "branchId")
    @JsonManagedReference
    Branch branch;
    @OneToOne(mappedBy = "managerId")
    @JsonBackReference
    Branch branchOneToOne;
    @OneToMany(mappedBy = "creditorId")
    @JsonBackReference
    List<CreditRequest> creditRequests;
}

Bankworker enitity must show: bankworker_id, Fname, Lname, Login, Password, branch_id

package com.educationalcenter.demo.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.*;
import lombok.experimental.FieldDefaults;

import javax.persistence.*;
import java.util.List;

@NoArgsConstructor
@AllArgsConstructor
@Builder
@Setter
@Getter
@FieldDefaults(level = AccessLevel.PRIVATE)
@Entity
@Table(name = "branch")
public class Branch {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long branchId;
    @Column(nullable = false, unique = true)
    String address;
    @Column(nullable = false)
    String city;
    String country;
    @OneToOne
    @JoinColumn(name = "manager_id", referencedColumnName = "bankworkerId", unique = true)
    @JsonManagedReference
    Bankworker managerId;
    @OneToMany(mappedBy = "branch")
    @JsonBackReference
    List<Bankworker> bankworkers;
}

Branch entity must show: branch_id, address, city, country, manager_id.

So, when I try to get with jpa findAll() methods all branches or bankworkers, it gives this:

[
    {
        "bankworkerId": 1,
        "branch": {
            "branchId": 1,
            "address": "Lev Tolstoy 3, 17",
            "city": "Bishkek",
            "country": "Kyrgyzstan",
            "managerId": {
                "bankworkerId": 1,
                "branch": {
                    "branchId": 1,
                    "address": "Lev Tolstoy 3, 17",
                    "city": "Bishkek",
                    "country": "Kyrgyzstan",
                    "managerId": {
                        "bankworkerId": 1,
                        "branch": {
                            "branchId": 1,
                            "address": "Lev Tolstoy 3, 17",
                            "city": "Bishkek",
                            "country": "Kyrgyzstan",
                            "managerId": {
                                "bankworkerId": 1,
                                      etc.

In short, it calls data about itself, so it never stops. I'd like to know how I can handle this without sacrificing any fields

  • This happens due to your bi-directional relationship between the objects. I would rather recommend introducing DTOs in your API instead of entity objects and some mapper logic, or in case of only needing to read the data, have a look at JPA projections and project the entity data into DTOs. – tbjorch May 09 '22 at 13:52
  • Refer https://stackoverflow.com/questions/31319358/jsonmanagedreference-vs-jsonbackreference – Deepanshu Rathi May 12 '22 at 11:38

0 Answers0