【问题标题】:Spring JPA many to many Json is showing a wrong data formatSpring JPA多对多Json显示错误的数据格式
【发布时间】:2020-12-11 17:02:23
【问题描述】:

我打算创建一个具有多对多关系的 API。

我试图输出的 JSON 如下。

    [
    {
        "id": 12,
        "firstname": "asdfghjkl ",
        "surname": "asda",
        "techniqueSet": [
            {
                "id": 0,
                "techniqueName": "java",
            },
            {
                "id": 1,
                "techniqueName": "C#",
                
                    }
                ]
     },
            
    {
        "id": 49,
        "firstname": "asc",
        "surname": "as",
        "techniqueSet": []
    },
    {
        "id": 90,
        "firstname": "JACOB",
        "surname": "LOL",
        "techniqueSet": [
            {
                "id": 0,
                "techniqueName": "Python",
            },
            {
                "id": 1,
                "techniqueName": "Haskell",
                
                    }
                ]
    },
    {
        "id": 92,
        "firstname": "Omar",
        "surname": "LOL",
        "techniqueSet": []
    }
]

但是,我得到以下信息。

[
    {
        "id": 12,
        "firstname": "asdfghjkl ",
        "surname": "asda",
        "techniqueSet": [
            {
                "id": 0,
                "techniqueName": "Java",
                "playersSet": [
                    12
                ]
            },
            {
                "id": 1,
                "techniqueName": "C#",
                "playersSet": [
                    12,
                    {
                        "id": 90,
                        "firstname": "Jacob",
                        "surname": "LOL",
                        "techniqueSet": [
                            1,
                            {
                                "id": 2,
                                "techniqueName": "Python",
                                "playersSet": [
                                    12,
                                    90
                                ]
                            },
                            {
                                "id": 3,
                                "techniqueName": "Haskell",
                                "playersSet": [
                                    90
                                ]
                            }
                        ]
                    }
                ]
            },
            2
        ]
    },
    {
        "id": 49,
        "firstname": "asc",
        "surname": "as",
        "techniqueSet": []
    },
    {
        "id": 90,
        "firstname": "LOL",
        "surname": "LOL",
        "techniqueSet": []
    },
    91,
    {
        "id": 92,
        "firstname": "Omar",
        "surname": "LOL",
        "techniqueSet": []
    }
]

我的 Java 以下是实体

@Entity
@Table(name = "players")
@EntityListeners(AuditingEntityListener.class)
@JsonIdentityInfo(
        generator = ObjectIdGenerators.PropertyGenerator.class,
        property = "id")
public class Players {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="player_id")
    private int id;
    private String firstname;
    private String surname;
    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(
            name = "able_to_use",
            joinColumns = {@JoinColumn(name = "player_id", referencedColumnName = "player_id")},
            inverseJoinColumns = {@JoinColumn(name = "technique_id", referencedColumnName = "id")}
    )
    private List<Technique> techniqueSet ;
    //Getters and setters

我的第二个实体

@Entity
@Table(name = "technique")
@EntityListeners(AuditingEntityListener.class)

@JsonIdentityInfo(
        generator = ObjectIdGenerators.PropertyGenerator.class,
        property = "id")
public class Technique {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @Column(name = "technique_name")
    private String techniqueName;
    @ManyToMany(mappedBy = "techniqueSet", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<Players> playersSet ;

    //Getters and Setter

你能帮忙吗?我是 JPA 框架的新手。我认为错误可能是每个实体中引用联接的方式。

【问题讨论】:

    标签: java mysql spring hibernate spring-data-jpa


    【解决方案1】:

    您应该在Technique 类中创建playersSet 属性transient 或使用@JsonIgnore

    这里发生的是递归序列化。 First Player 正在序列化,然后进入 Technical 类,但 Technique 类也有其领域的玩家。所以这也将被序列化(再次)。

    有关 Jackson 注释的更多信息,请阅读此simple intro

    有关 Java 中瞬态的更多信息,请阅读 article

    【讨论】:

    • 我现在面临的问题。似乎如果技术集已经具有该值,如果假设另一个玩家需要相同的值,它将不会再次打印 JSON。你知道如何解决这个问题吗?
    猜你喜欢
    • 2017-06-12
    • 1970-01-01
    • 2019-03-27
    • 2017-12-12
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    相关资源
    最近更新 更多