简体   繁体   English

动态获取关联实体

[英]Dynamically fetch associated entities

I have 3 entities 我有3个实体

Project.java Project.java

@Entity
public class Project {
    @Id
    @GeneratedValue
    private Long id;

    @Column(nullable = false)
    private String name;

    @OneToMany(mappedBy = "project", fetch = FetchType.LAZY)
    private List<ProjectReview> projectReviews = new ArrayList<>();

    // getters
}

User.java User.java

@Entity
public class User {

    @Id
    @GeneratedValue
    private Long id;

    @Column(nullable = false)
    private String userName;

    @OneToMany(mappedBy = "reviewer")
    private List<ProjectReview> projectReviews = new ArrayList<>();

    // getters
}

ProjectReview.java ProjectReview.java

@Entity
@IdClass(ProjectRevieweId.class)
public class ProjectReview {

    @Id
    private long projectId;

    @Id
    private long userId;

    @Column(nullable = false)
    private String review;

    @ManyToOne
    @JoinColumn(name = "userId", insertable = false, updatable = false)
    private User reviewer;

    @ManyToOne
    @JoinColumn(name = "projectId", insertable = false, updatable = false)
    private Project project;

    // getters
}

Pretty simple many-to-many relationship with join table. 与联接表非常简单的多对多关系。 This setup is not working, because when serialized with jackson to json format, it has infinite depth EVEN if the default fetch type is LAZY on collections (i dont understand why!?). 此设置无法正常工作,因为当用杰克逊序列化为json格式时, 即使集合的默认获取类型为LAZY ,它的深度也是无限的(我不明白为什么!)。

I am using standard Spring Repository->Service->RestController flow with Spring Boot 1.4.1 on MySQL . 我在MySQL上使用Spring Boot 1.4.1标准Spring Repository->Service->RestController流。

I used the @JsonBackReference on ProjectReview.reviewer and ProjectReview.project but thats not what I want, because sometimes i want to have access to associated entities, and sometimes not. 我在ProjectReview.reviewerProjectReview.project上使用了@JsonBackReference ,但这不是我想要的,因为有时我想访问关联的实体,有时却不想。 Explanation folllows: 说明如下:

When I call rest service GET ../projects, i would like to see 当我打电话给REST服务GET ../projects时,我想看看

[{
    "id":1,
    "name":"project1",
    "projectReviews":[{
        "review":"My super review!",
        "reviewer":{ -- this has to be included
            "id":1,
            "userName":"user1",
            "projectReviews":null -- this cant be fetched as it causes recursion
            },
        "project":{ -- instance or null or entirely missing this attribute - as it is the same as root
            "id":1,
            "name":"project1",    
            "projectReviews":null -- this cant be fetched as it causes recursion        
            }
        },
        {
            -- second review...
        }]
    },{
            -- second project...
    },
    ...etc
]

But when I call GET ../users, i would like to see 但是当我打电话给GET ../users时,我想看看

[{
    "id":1,
    "userName":"user1",
    "projectReviews":[{
        "review":"My super review!",
        "reviewer":{ -- instance or null or entirely missing this attribute - as it is the same as root
            "id":1,
            "userName":"user1",
            "projectReviews":null -- this cant be fetched as it causes recursion
            },
        "project":{ -- this has to be included
            "id":1,
            "name":"project1",    
            "projectReviews":null -- this cant be fetched as it causes recursion        
            }       
        },
        {
            -- second review...       
        }]
    },{ 
        -- second user
    }
        ...etc
]

I hope you get it. 希望您能理解。 projectReviews on top level should be eagerly fetched, but on second level they should not - beacuse it creates infinite depth structure. 应该急切地获取顶层的projectReviews ,但在第二层则不应这样做-因为它会创建无限的深度结构。

How could I setup the fetching or entities to provide this king of structure? 如何设置获取或实体来提供这种结构之王?

Bonus question - Why are projectReviews fetched in json if default is LAZY fetching? 奖金问题-如果默认为LAZY提取,为什么要在json中提取projectReviews

You can use follwoing annotations to solve the infinite recursion problem : @JsonManagedReference and @JsonBackReference. 您可以使用以下注解来解决无限递归问题:@JsonManagedReference和@JsonBackReference。

Use @JsonManagedReference in User.java and Project.java for variable "projectReviews" and @JsonBackReference for "reviewer" and "project" 将User.java和Project.java中的@JsonManagedReference用于变量“ projectReviews”,将@JsonBackReference用于“ reviewer”和“ project”

If you are not able to get reviewer and project under projectReviews, please use the annotations othey way around, @JsonManagedReference in projectReviews and vice versa. 如果您无法在projectReviews下获得审阅者和项目,请使用其他方式注释,在projectReviews中使用@JsonManagedReference,反之亦然。

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

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