【问题标题】:spring-data-rest, manytomany relation with join tablespring-data-rest,与连接表的多对多关系
【发布时间】:2014-03-18 00:38:10
【问题描述】:

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

我正在尝试在 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 调用;

[GET http://localhost:8080/webapp/users/1]

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

【问题讨论】:

标签: java spring spring-data spring-data-jpa spring-data-rest


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-11-01
    • 2019-01-29
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    相关资源
    最近更新 更多