【问题标题】:How not to get the colletion in @ManyToMany in hibernate/springboot?如何不在休眠/春季启动中获取@ManyToMany 中的集合?
【发布时间】:2020-01-10 21:30:34
【问题描述】:

我有 3 张桌子,公司、优惠券、客户。 很多公司应该有很多优惠券,很多客户应该有很多优惠券。

一切正常,除了我不想在打电话给优惠券/客户时收集优惠券。

我正在使用 Swagger 测试应用程序,并且我正在获取客户/公司的优惠券集合。

我确实尝试添加 LAZY 提取类型,但它不起作用,我实际上不确定如何调用它。

我不想在致电公司时获得优惠券集合。

@Entity
public class Company {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column
private long id;
@ManyToMany
private List<Coupon> coupons;
private String name;
private String email, password;

public Company() {

}

public Company(long id, String name, String email, String password) {
    this.id = id;
    this.name = name;
    this.email = email;
    this.password = password;
}


@Entity
public class Coupon {

@Id
@GeneratedValue
@Column
private long id;
private String title;
private String message;
private double price;
private int amount;

@Enumerated(EnumType.STRING)
@Column(columnDefinition = "varchar(32) default 'OTHER'")
private CouponType type = CouponType.OTHER;
@Enumerated(EnumType.STRING)
@Column(columnDefinition = "varchar(32) default 'SALE'")
private CouponStatus status = CouponStatus.SALE;

这是我在招呼一家公司时得到的 JSON

{
  "id": 2,
  "coupons": [],
  "name": "Macdonalds",
  "email": "Macdonalds",
  "password": "123"
}

【问题讨论】:

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


    【解决方案1】:

    如果不希望在创建json时被序列化(springboot中默认),需要排除。在 jackson 中,只需用 @JsonIgnore 注释它:

    @JsonIgnore
    @ManyToMany
    private List<Coupon> coupons;
    

    另一方面,在 gson 中,您必须使用 @Expose 注释每个其他字段。

    【讨论】:

    • 试过了,但是我刚开这个账号,信誉不够:(
    • 是的,但我需要再等 4 分钟,我正在刷新页面 :)!
    猜你喜欢
    • 2015-06-30
    • 2020-05-17
    • 1970-01-01
    • 2018-04-25
    • 2020-10-21
    • 1970-01-01
    • 2018-01-23
    • 2020-05-03
    • 1970-01-01
    相关资源
    最近更新 更多