Spring -data-rest, manytomany relationship with join table

Is it possible to identify the multi-valued relationship that the union object uses (which contains additional columns of data), below are my entities,

I am trying to get “purchases” to show in REST, I have included “products” as an example of a working REST display;

@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;

}

So, if I send a REST call,

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

It returns links for [http: // localhost: 8080 / webapp / users / 1 / products], but not for [http: // localhost: 8080 / webapp / users / 1 / purchases]

+4
source share
1 answer

, ; JpaRepository Purchase. , , REST .

+3

Source: https://habr.com/ru/post/1527480/


All Articles