使用@OneToMany时,JPA结果无限循环

时间:2019-11-13 04:36:40

标签: java spring-boot jpa

我有两个实体,分别为Course和Topic。这是一对多的关系。当我得到所有课程的清单时,我得到了奇怪的结果。看来它正在获得较长的无限循环结果(我得到的JSON响应只是其中的一部分,但它一直在不断发展)

Picture of my ERD

[
    {
        "id": "Java",
        "name": "Java",
        "descr": "This course goes over Java",
        "topic": [
            {
                "id": "java-core",
                "name": "Java core",
                "descr": "This topic goes over java core",
                "course": {
                    "id": "Java",
                    "name": "Java",
                    "descr": "This course goes over Java",
                    "topic": [
                        {
                            "id": "java-core",
                            "name": "Java core",
                            "descr": "This topic goes over java core",
                            "course": {
                                "id": "Java",
                                "name": "Java",
                                "descr": "This course goes over Java",
                                "topic": [
                                    {
                                        "id": "java-core",
                                        "name": "Java core",
                                        "descr": "This topic goes over java core",
                                        "course": {
                                            "id": "Java",
                                            "name": "Java",
                                            "descr": "This course goes over Java",
                                            "topic": [
                                                {
                                                    "id": "java-core",
                                                    "name": "Java core",
                                                    "descr": "This topic goes over java core",
                                                    "course": {
                                                        "id": "Java",
                                                        "name": "Java",
                                                        "descr": "This course goes over Java",
                                                        "topic": [
                                                            {
                                                                "id": "java-core",
                                                                "name": "Java core",
                                                                "descr": "This topic goes over java core",

1 个答案:

答案 0 :(得分:1)

使用

@JsonManagedReference实例化的第一个对象的注释

@JsonBackReference实例化的第二个对象的注释