为购物车项目和产品设置正确的 jpa 映射
我正在通过一些示例学习jpa,涉及购物车和购物车项目。我将它们定义如下..但不太确定要使用哪个映射
@Entity
class Product{
private Long id;
private String name;
...
}
@Entity
class CartItem{
private Long id;
private Product product;
private int quantity;
...
}
@Entity
class ShoppingCart{
private Long id;
@OneToMany
private Set<CartItem> cartItems;
...
}
我不太确定的是如何在 Product 之间关联和 CartItem
以及如何设置 mappedBy
属性。有人可以告诉我如何执行此操作吗?执行此操作的标准是什么?我试图设置类似 1 cartitem 的内容包含 1 个产品听起来像是一个
和 OneToOne
关系。如果是这样,谁维护这个关系(这不是 mappedBy
所做的吗?)。我对 < code>ShoppingCartCartItem
也
提前致谢
I am learning jpa through some examples ,involving a shopping cart and cart items.I defined them as below..but am not very sure about which mapping to use
@Entity
class Product{
private Long id;
private String name;
...
}
@Entity
class CartItem{
private Long id;
private Product product;
private int quantity;
...
}
@Entity
class ShoppingCart{
private Long id;
@OneToMany
private Set<CartItem> cartItems;
...
}
What I am not very sure of ,is how to relate between Product and CartItem
and how to set the mappedBy
attribute.Can somebody tell me how to do this?What are the criteria in doing this?I was trying to set something like 1 cartitem contains 1 product only
.It sounds like a OneToOne
relation.If so, who maintains the relation(is that not what mappedBy
does?).I have similar doubt about ShoppingCart
and CartItem
s too
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一件购物车商品引用一种产品。但单个产品被多个购物车项目引用。所以这是一个一对多的关联。
一个购物车作为多个商品,而一个商品是一个购物车的一部分,因此它也是一对多关联。
当您有双向 OneToMany 关联时,关联的所有者端始终是多端。关联的所有者一侧是没有
mappedBy
属性的一侧。事实上,mappedBy
的意思是“我只是关联的另一端,它已经由以下属性映射了”。请注意,关联的映射方式(连接列、连接表)必须仅在所有者端定义,其中mappedBy
属性不存在。当您具有单向关联时,只有一个位置可以定义映射,因此永远不会使用mappedBy 属性。
因此,您的实体应该像这样映射:
如果您希望您的购物车项目了解其所属的购物车,您的关联将变为双向,并且实体将变为:
One cart item references one product. But a single product is referenced by several cart items. So it's a one-to-many association.
One shopping cart as several items, and an item is part of one shopping cart, so it's also a one-to-many association.
When you have a bidirectional OneToMany association, the owner side of the association is always the many side. The owner side of an association is the side where there is no
mappedBy
attribute. Indeed,mappedBy
means "I'm just the other side of an association, which is already mapped by the following attribute". Note that the way the asociation is mapped (join column, join table) must only be defined on the owner side, where themappedBy
attribute is absent.When you have a unidirectional association, there is only one place where the mapping can be defined, and the mappedBy attribute is thus never used.
So, your entities should be mapped like this:
If you want your cart item to know about its owning shopping cart, your association becomes bidirectional, and the entities become:
您应该首先决定是想要单向还是双向
@OneToOne
关系。单向关系具有拥有方(存在对另一个类的引用的一方),而双向关系同时具有拥有方和反向方。如果您声明单向
@OneToOne
关系,则无需担心mappedBy
属性。例如,如果您将Product
-CartItem
关系声明为单向关系,且CartItem
拥有该关系(并且还拥有数据库),那么您只需要在Product
类中注释对Product
的引用即可,如下所示;Product
类中不需要进行任何更改:如果您需要双向关系,则必须定义双向引用。在这种情况下,您需要添加对
Product
类中的CartItem
的引用。此外,您需要在反面指定mappedBy
属性,以引用拥有方(数据库中具有外键的一方),如下所示:You ought to first decide whether you want a unidirectional or a bidirectional
@OneToOne
relationship. A unidirectional relationship has an owning side (the side where the reference to another class is present), while a bidirectional relationship has both an owning side and an inverse side.If you are declaring a unidirectional
@OneToOne
relationship, then you do not need to worry about themappedBy
attribute. For example, if you declare theProduct
-CartItem
relationship as a unidirectional relationship with theCartItem
owning the relationship (and also possessing the foreign key in the database), then you merely need to annotate a reference to theProduct
in theProduct
class, as shown below; no changes are required in theProduct
class:If you need a bi-directional relationship, then you must define references that go both ways. In this case, you'll need to add a reference to a
CartItem
from theProduct
class. Furthermore, you'll need to specify themappedBy
attribute on the inverse side, to refer to the owning side (the side that has the foreign key in the database), as shown below: