Looping problem when serializing objects to JSON in Spring Boot

I’m using Spring Boot 3.2 version with MySQL database. I have two objects

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Getter
@Setter
@Entity
@Table(name = "shops")
public class Shop{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String address;
@OneToMany(mappedBy = "shop", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Book> books;
}
</code>
<code>@Getter @Setter @Entity @Table(name = "shops") public class Shop{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String address; @OneToMany(mappedBy = "shop", cascade = CascadeType.ALL, fetch = FetchType.LAZY) private List<Book> books; } </code>
@Getter
@Setter
@Entity
@Table(name = "shops")
public class Shop{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String address;
    @OneToMany(mappedBy = "shop", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<Book> books;
}

and

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Getter
@Setter
@Entity
@Table(name = "books")
public class Book{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String description;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "shop_id")
private Shop shop;
}
</code>
<code>@Getter @Setter @Entity @Table(name = "books") public class Book{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String description; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "shop_id") private Shop shop; } </code>
@Getter
@Setter
@Entity
@Table(name = "books")
public class Book{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String description;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "shop_id")
    private Shop shop;
}

So I have some lopping problem when I’m trying to get all Shops with all books. I have object Shop with all Books object inside each book has his own shop object inside with all book etc. And it looks like

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[
{
"id": 4,
"address": "Street",
"books": [
{
"id": 3,
"description": "Book1",
"shop" : {
"id": 4,
"address": "Street"
"books": [
{
"id": 3,
"description": "Book1",
"shop" : { ...
}
]
}
]
}
]
</code>
<code>[ { "id": 4, "address": "Street", "books": [ { "id": 3, "description": "Book1", "shop" : { "id": 4, "address": "Street" "books": [ { "id": 3, "description": "Book1", "shop" : { ... } ] } ] } ] </code>
[
    {
        "id": 4,
        "address": "Street",
        "books": [
                {
                "id": 3,
                "description": "Book1",
                "shop" : {
                       "id": 4,
                       "address": "Street"
                       "books": [
                               {
                                "id": 3,
                                "description": "Book1",
                                "shop" : { ...
                               }
                       ]
                }
        ]
     }
]

So the problem is that I want to use one class for saving books/users and geting from database.
Then I need that user have all books as objects with all data but books have only userId.
So getting all shops should look like this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[
{
"id": 4,
"address": "Street",
"books": [
{
"id": 3,
"description": "Book1",
"shop" : 4
}
]
}
]
</code>
<code>[ { "id": 4, "address": "Street", "books": [ { "id": 3, "description": "Book1", "shop" : 4 } ] } ] </code>
[
    {
        "id": 4,
        "address": "Street",
        "books": [
                {
                "id": 3,
                "description": "Book1",
                "shop" : 4
                }
         ]
     }
]

and geeting all books should look like this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[
{
"id": 3,
"description": "Book1",
"shop": 4
},
{
"id": 4,
"description": "Book2",
"shop": 5
},
{
"id": 5,
"description": "Book4",
"shop": 5
},
]
</code>
<code>[ { "id": 3, "description": "Book1", "shop": 4 }, { "id": 4, "description": "Book2", "shop": 5 }, { "id": 5, "description": "Book4", "shop": 5 }, ] </code>
[
    {
        "id": 3,
        "description": "Book1",
        "shop": 4
    },
    {
        "id": 4,
        "description": "Book2",
        "shop": 5
    },
    {
        "id": 5,
        "description": "Book4",
        "shop": 5
    },
]

As I said I want to use same class for saving shop/books and same class for getting.
I faced with couple problems.

  1. I dont want to create additional ShopDTO/BookDTO objects for getting because if it will be 1M books then I need to created 1M objects each time while getting.
  2. I can’t add just one more variable such as Long shopId; because it will be hibernate error
    Table [books] contains physical column name [shop_id] referred to by multiple logical column names: [shop_id], [shopId]
  3. I can’t change
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "shop_id")
private Shop shop;
</code>
<code>@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "shop_id") private Shop shop; </code>
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "shop_id")
private Shop shop; 

on

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "shop_id")
private Long shop;
</code>
<code>@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "shop_id") private Long shop; </code>
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "shop_id")
private Long shop;

or remove @ManyToOne at all.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật