SpringBoot:通过不同的GET请求参数化解析实体关系

时间:2020-03-29 12:25:37

标签: json spring-boot spring-data-jpa spring-rest

我想通过对实体的不同GET请求对获取和返回实体关系进行参数化。 是否有结合获取策略和json包含的配置方法来实现这一目标?

示例:如何根据A上的不同Gets返回/不返回B列表。

@Entity
public class A {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToMany(mappedBy = "a", fetch = FetchType.LAZY)
    private List<B> listBs ; 
}

@Entity
public class B {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToOne(fetch = FetchType.LAZY)
    private A a ; 
}

1 个答案:

答案 0 :(得分:0)

您可以使用@JsonManagedReference@JsonBackReference

@Entity
public class A {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToMany(mappedBy = "a", fetch = FetchType.LAZY)
    @JsonBackReference
    private List<B> listBs ; 
}

@Entity
public class B {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToOne(fetch = FetchType.LAZY)
    @JsonManagedReference
    private A a ; 
}