为购物车项目和产品设置正确的 jpa 映射

发布于 2024-12-03 04:05:48 字数 687 浏览 1 评论 0原文

我正在通过一些示例学习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 CartItems too

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魔法唧唧 2024-12-10 04:05:49

一件购物车商品引用一种产品。但单个产品被多个购物车项目引用。所以这是一个一对多的关联。

一个购物车作为多个商品,而一个商品是一个购物车的一部分,因此它也是一对多关联。

当您有双向 OneToMany 关联时,关联的所有者端始终是端。关联的所有者一侧是没有 mappedBy 属性的一侧。事实上,mappedBy 的意思是“我只是关联的另一端,它已经由以下属性映射了”。请注意,关联的映射方式(连接列、连接表)必须仅在所有者端定义,其中 mappedBy 属性不存在。

当您具有单向关联时,只有一个位置可以定义映射,因此永远不会使用mappedBy 属性。

因此,您的实体应该像这样映射:

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

  ...
}

如果您希望您的购物车项目了解其所属的购物车,您的关联将变为双向,并且实体将变为:

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

  ...
}

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 the mappedBy 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:

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

  ...
}
北方的韩爷 2024-12-10 04:05:49

您应该首先决定是想要单向还是双向 @OneToOne 关系。单向关系具有拥有方(存在对另一个类的引用的一方),而双向关系同时具有拥有方和反向方。

如果您声明单向 @OneToOne 关系,则无需担心 mappedBy 属性。例如,如果您将 Product-CartItem 关系声明为单向关系,且 CartItem 拥有该关系(并且还拥有数据库),那么您只需要在 Product 类中注释对 Product 的引用即可,如下所示; Product 类中不需要进行任何更改:

@Entity
class CartItem{
   private Long id;

   @OneToOne
   private Product product;

   private int quantity;

...
}

如果您需要双向关系,则必须定义双向引用。在这种情况下,您需要添加对 Product 类中的 CartItem 的引用。此外,您需要在反面指定 mappedBy 属性,以引用拥有方(数据库中具有外键的一方),如下所示:

@Entity
class Product{

   private Long id;
   private String name;
   @OneToOne(mappedBy="product") // the name of the property in the owning side
   private CartItem cartItem;
   ...
}

@Entity
class CartItem{
   private Long id;

   @OneToOne                    //the owning side
   private Product product;

   private int quantity;

...
}

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 the mappedBy attribute. For example, if you declare the Product-CartItem relationship as a unidirectional relationship with the CartItem owning the relationship (and also possessing the foreign key in the database), then you merely need to annotate a reference to the Product in the Product class, as shown below; no changes are required in the Product class:

@Entity
class CartItem{
   private Long id;

   @OneToOne
   private Product product;

   private int quantity;

...
}

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 the Product class. Furthermore, you'll need to specify the mappedBy attribute on the inverse side, to refer to the owning side (the side that has the foreign key in the database), as shown below:

@Entity
class Product{

   private Long id;
   private String name;
   @OneToOne(mappedBy="product") // the name of the property in the owning side
   private CartItem cartItem;
   ...
}

@Entity
class CartItem{
   private Long id;

   @OneToOne                    //the owning side
   private Product product;

   private int quantity;

...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文