spring-data-rest,与join table的多种关系

时间:2014-02-18 10:22:10

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

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

我正试图在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]

的链接

1 个答案:

答案 0 :(得分:3)

解决了问题所在;我需要为Purchase实体创建一个JpaRepository。我添加的时候,可以使用购买的REST链接。