簡體   English   中英

動態獲取關聯實體

[英]Dynamically fetch associated entities

我有3個實體

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

@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

@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
}

與聯接表非常簡單的多對多關系。 此設置無法正常工作,因為當用傑克遜序列化為json格式時, 即使集合的默認獲取類型為LAZY ,它的深度也是無限的(我不明白為什么!)。

我在MySQL上使用Spring Boot 1.4.1標准Spring Repository->Service->RestController流。

我在ProjectReview.reviewerProjectReview.project上使用了@JsonBackReference ,但這不是我想要的,因為有時我想訪問關聯的實體,有時卻不想。 說明如下:

當我打電話給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
]

但是當我打電話給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
]

希望您能理解。 應該急切地獲取頂層的projectReviews ,但在第二層則不應這樣做-因為它會創建無限的深度結構。

如何設置獲取或實體來提供這種結構之王?

獎金問題-如果默認為LAZY提取,為什么要在json中提取projectReviews

您可以使用以下注解來解決無限遞歸問題:@JsonManagedReference和@JsonBackReference。

將User.java和Project.java中的@JsonManagedReference用於變量“ projectReviews”,將@JsonBackReference用於“ reviewer”和“ project”

如果您無法在projectReviews下獲得審閱者和項目,請使用其他方式注釋,在projectReviews中使用@JsonManagedReference,反之亦然。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM