gpt4 book ai didi

java - spring-data-rest,与连接表的多对多关系

转载 作者:搜寻专家 更新时间:2023-11-01 03:06:05 25 4
gpt4 key购买 nike

是否可以公开使用连接实体(包含额外数据列)的多对多关系,下面是我的实体;

我正在尝试让“购买”在 REST 中显示,我将“产品”作为工作 REST 映射的示例;

@Entity
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, targetEntity = Purchase.class, orphanRemoval = true)
@JoinColumn(name = "user_id", updatable = false)
private List<Purchase> purchases = new ArrayList<>();

@ManyToMany
@JoinColumn(name = "user_id", updatable = false)
private List<Product> products = new ArrayList<>();

}

@Entity
public class Product {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String name;

}

@Entity
public class Purchase implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;

@ManyToOne
@JoinColumn(name = "user_id", referencedColumnName = "id")
private User user;

@ManyToOne(targetEntity = Prodect.class)
@JoinColumn(name = "product_id", referencedColumnName = "id")
private Product product;

@Column(name = "purchase_date")
private Date purchaseDate;

}

所以如果我发送 REST 调用;

[获取 http://localhost:8080/webapp/users/1]

它返回 [http://localhost:8080/webapp/users/1/products] 的链接,但不返回 [http://localhost:8080/webapp/users/1/purchases] 的链接

最佳答案

找出问题所在;我需要为 Purchase 实体创建一个 JpaRepository。我添加后不久,用于购买的 REST 链接就可用了。

关于java - spring-data-rest,与连接表的多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21850722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com