指定一个以上表的一对多关系

时间:2019-02-18 09:49:09

标签: hibernate jpa spring-data-jpa

我有三个桌子

1).Category(id,CategoryName)

2).Items(id,imageUrl,name,price,units)

3).Shoppingcart(id,dateCreated)

使用案例是

1)一个类别可以有多个项目

2)一个购物车可以有多个物品。

所以我的问题是

1)商品在Category和ShoppingCart之间共享。如何在休眠状态下指定实体关系。

2)如果我必须针对物料指定购物车的数量 即购物车中的商品数量。 如何在它们之间创建实体关系。

1 个答案:

答案 0 :(得分:0)

这是这种关系的样子:

public class Item {

    private int id;

    // all other fields

    @ManyToOne
    @JoinColumn
    private Category category;

    @ManyToOne
    @JoinColumn
    private ShoppingCart shoppingCart;

    /*.........
    Setter Getters
    */


}


public class Category {

    private int id;

    // all other fields

    @OneToMany(mappedBy = "bookCategory", cascade = CascadeType.REMOVE)
    private Set<Item> items;

    /*.........
    Setter Getters
    */


}

public class ShoppingCart {

    private int id;

    // all other fields

    @OneToMany(mappedBy = "shoppingCart", cascade = CascadeType.REMOVE)
    private Set<Item> items;

    /*.........
    Setter Getters
    */


}
相关问题