购物车应用程序中类(CartItem 和 OrderItem)设计的冗余
我正在尝试使用 jpa 在 java 中设计和编写一个基本的商店 Web 应用程序。 当用户在 ShoppingCart
中选择产品时,我会创建一个 Cartitem
列表。 Cartitem 的生命周期与购物车相关。删除购物车后,其所有的 caritem 都必须消失。 (用户购买完成后,购物车将被删除) 之后需要将这些选择转换为订单。我在每个Order
中创建了一个OrderItem
列表。然后我发现每个Caritem都有一个对应的OrderItem。 订单物品的生命周期与订单相关。
我是 java/javaee 的初学者..我想知道就设计而言我是否走在正确的轨道上.. 这是设计此类应用程序的正确方法吗? CartItem
和 OrderItem
类看起来太相似,我想知道是否有任何冗余。
我的实体类现在看起来像这样。
class ShoppinCart{
@OneToMany(orphanRemoval=true, cascade={CascadeType.ALL},mappedBy="order")
private List<CartItem> citems;
...
}
class CartItem{
Product product;
int quantity;
...
@ManyToOne
ShoppingCart cart;
}
class Order{
@OneToMany(orphanRemoval=true, cascade={CascadeType.ALL},mappedBy="order")
private List<OrderItem> orderItem;
private Date orderDate;
private Customer customer;
}
class OrderItem{
Product product;
int quantity;
...
@ManyToOne
Order order;
}
I am trying to design and code a basic store web application in java using jpa.
When a user selects products in a ShoppingCart
,I create a list of Cartitem
s.
The Cartitem's lifespan is tied to the cart.When a cart is deleted all its cartitems must go.
(The cart is deleted after a user finishes purchasing)
Afterwards these selections need to be converted into an order.I created a list of OrderItem
s in each Order
.Then I found that each Cartitem has a corresponding OrderItem.
The lifespan of a Orderitem is tied to the Order.
I am a beginner at java/javaee..I would like to know if I am on the right track as far as the design is concerned..
Is this the correct way to design such an application.The CartItem
and OrderItem
classes look too much alike ,that I am wondering if there is any redundancy.
My entity classes now look like this.
class ShoppinCart{
@OneToMany(orphanRemoval=true, cascade={CascadeType.ALL},mappedBy="order")
private List<CartItem> citems;
...
}
class CartItem{
Product product;
int quantity;
...
@ManyToOne
ShoppingCart cart;
}
class Order{
@OneToMany(orphanRemoval=true, cascade={CascadeType.ALL},mappedBy="order")
private List<OrderItem> orderItem;
private Date orderDate;
private Customer customer;
}
class OrderItem{
Product product;
int quantity;
...
@ManyToOne
Order order;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的 CartItem 和 OrderItem 是多余的。您不需要在 CartItem 内存储购物车引用。
在我看来,购物车只是一个尚未最终确定的订单。您可能需要为订单添加状态:待处理、已发货、已取消等,因此购物卡可能只是处于待处理状态的订单。如果您需要一个单独的类,那么只需将其包装起来,这样 ShoppingCart 就有一个订单(并且没有 CartItem 列表)。
Yes CartItem and OrderItem are redundant. You don't need to store a cart reference inside the CartItem.
In my opinion a ShoppingCart is just an order which hasn't been finalized. You'll probably need to add a status to your order, pending, dispatched, cancelled etc, so the shopping card could be just an order with a pending status. If you need a separate class then just wrap it so a ShoppingCart has one Order (and no CartItem list).
我会创建通用超类 SomeItem,如果您需要一些附加信息(如 itemDeliveryDate),请将其添加到子类中。将 CartItem 作为 OrderItem 构造函数的参数 会很有帮助。
此外,也不需要从项目到持有对象(购物车或订单)的导航性,删除 @ManyToOne 标记属性。
I'd create common superclas SomeItem and if you need some additional info (like itemDeliveryDate) add it to the child class. Having CartItem as parameter to OrderItem constructor would be helpfull.
Also there is no need for navigability from items to the holdin object (Cart or Order), remove the @ManyToOne marked properties.