简体   繁体   English

使用@OneToMany 时 JPA 导致无限循环

[英]JPA result infinite loop when using @OneToMany

I have two entities called Course and Topic.我有两个实体,称为课程和主题。 It is a One-to-Many relationship.这是一对多的关系。 When I get list of all Courses, I get weird results.当我得到所有课程的列表时,我得到了奇怪的结果。 It looks like it is getting a long infinite loop results (The JSON response that I got is just part of it but it keeps going and going)看起来它正在获得一个很长的无限循环结果(我得到的 JSON 响应只是其中的一部分,但它会一直持续下去)

Picture of my ERD我的 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",

Use利用

@JsonManagedReference annotation for the first objects instantiated实例化的第一个对象的@JsonManagedReference注释

@JsonBackReference annotation for the second objects instantiated实例化的第二个对象的@JsonBackReference注释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM