3

schema table schemas There are two tables, they can be seen in the diagram. There is also a JPA-style description of these entities:

Entity for Gift

@Entity
public class Gift {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "title")
    private String title;

    // ... 

    @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE},
            orphanRemoval = true,
            fetch = FetchType.LAZY,
            mappedBy = "gift")
    private List<Image> images;
}

Entity fot Image

@Entity
public class Image {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "gift_id")
    private Gift gift;
    
    // ...

}

When executing the following JPA Query:

@Override
public List<Gift> getByFilter(FilterSearch filter) {
    String sql = """
            FROM Gift WHERE id = 2
            """;
    return entityManager.createQuery(sql, Gift.class).getResultList();
}

Result: It turns out a cyclic dependency, if you can call it that: I get a list gift, in which image, in which the same gift has the same images, which in turn have the same gift's, and so on ad infinitum.

[
    {
        "id": 2,
        "title": "title for gift",
        "description": "some simple description for gift",
        "shortDescription": "short description",
        "category": {
            "id": 1,
            "name": "first category",
            "parent": null
        },
        "costPrice": 2.0,
        "price": 3.4,
        "dateAdded": "2023-06-26T15:41:55.958+00:00",
        "isEnabled": false,
        "images": [
            {
                "id": 6,
                "gift": {
                    "id": 2,
                    "title": "title for gift",
                    "description": "some simple description for gift",
                    "shortDescription": "short description",
                    "category": {
                        "id": 1,
                        "name": "first category",
                        "parent": null
                    },
                    "costPrice": 2.0,
                    "price": 3.4,
                    "dateAdded": "2023-06-26T15:41:55.958+00:00",
                    "isEnabled": false,
                    "images": [
                        {
                            "id": 6,
                            "gift": {
                                "id": 2,
                                "title": "title for gift",
                                "description": "some simple description for gift",
                                "shortDescription": "short description",
                                "category": {
                                    "id": 1,
                                    "name": "first category",
                                    "parent": null
                                },
                                "costPrice": 2.0,
                                "price": 3.4,
                                "dateAdded": "2023-06-26T15:41:55.958+00:00",
                                "isEnabled": false,
                                "images": [
                

and so on ad infinitum.

Question. How to break this cycle?

0 Answers0