为购物车商品和产品设置正确的jpa映射 [英] setting the correct jpa mapping for shopping cart items and product
问题描述
我正在通过一些示例来学习jpa,涉及到购物车和购物车项目.我对它们的定义如下..但是我不确定要使用哪个映射
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;
...
}
我不是很确定Product and CartItem
与如何设置mappedBy
属性之间的联系.有人可以告诉我该怎么做吗?这样做的标准是什么?设置类似1 cartitem contains 1 product only
的东西.听起来像是OneToOne
关系,如果是这样,谁来维护该关系(不是mappedBy
所做的吗?)我对ShoppingCart
和CartItem
s也有类似的疑问
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
预先感谢
推荐答案
一个购物车商品引用了一种产品.但是单个产品被多个购物车项目引用.因此,这是一对多的关联.
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.
当您具有双向OneToMany关联时,关联的所有者端始终是许多端.关联的所有者端是没有mappedBy
属性的端.实际上,mappedBy
的意思是"我只是关联的另一端,已经由以下属性映射了".请注意,关联的映射方式(联接列,联接表)只能在不具有mappedBy
属性的所有者侧进行定义.
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 the mappedBy
attribute is absent.
具有单向关联时,只有一个地方可以定义映射,因此从不使用mapledBy属性.
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:
@Entity
class Product{
private Long id;
private String name;
...
}
@Entity
class CartItem{
private Long id;
@ManyToOne
private Product product;
private int quantity;
...
}
@Entity
class ShoppingCart{
private Long id;
@OneToMany
private Set<CartItem> cartItems;
...
}
如果您希望购物车商品知道其拥有的购物车,则关联将变为双向,并且实体将变为:
If you want your cart item to know about its owning shopping cart, your association becomes bidirectional, and the entities become:
@Entity
class CartItem{
private Long id;
@ManyToOne
private Product product;
@ManyToOne
private ShppingCart shoppingCart;
private int quantity;
...
}
@Entity
class ShoppingCart{
private Long id;
@OneToMany(mappedBy = "shoppingCart")
private Set<CartItem> cartItems;
...
}
这篇关于为购物车商品和产品设置正确的jpa映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!