What is an order in e-commerce application and how to control it. How is it related with user cart?

I'm training to create my first Food ordering from catering establishments application. I created administrator and his ability to add, edit and delete catering establishments - canteens, cafes, bars etc. Also to add various menu types and dishes into it. To edit it and to delete it. I have also created cart for users to add dishes from menus., edit quantity or to removes products from cart. But now I have to create an order. I kinda know what it is, but I don't know how exactly is it related with other entities. I know that each order should be for each user's cart at that exact time when he presses Checkout or order button. Then in the database there should be an order and all related information about the user and his ordered goods - dishes, quantities, price etc. What is relation between order and cart. For example my cart entity is this:
@Entity
@Table(name="cart_items")
public class CartItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@ManyToOne
@JoinColumn(name = "dish_id")
private Dish dish;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

private Integer quantity;

public CartItem() {}

public CartItem(Dish dish, User user, Integer quantity) {
super();
this.dish = dish;
this.user = user;
this.quantity = quantity;
}
@Entity
@Table(name="cart_items")
public class CartItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@ManyToOne
@JoinColumn(name = "dish_id")
private Dish dish;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

private Integer quantity;

public CartItem() {}

public CartItem(Dish dish, User user, Integer quantity) {
super();
this.dish = dish;
this.user = user;
this.quantity = quantity;
}
Manager or admin should be able to confirm or cancel the order. I think for the order I will use enum with values such as submitted, approved, withdrawn
21 Replies
JavaBot
JavaBot3y ago
Hey, @Tomasm21! Please remember to /close this post once your question has been answered!
Tomasm21
Tomasm21OP3y ago
I don't want to reveal User object information that contains password. I will have to use intermediate DTO. But my main question is how to define an order. With what entities it should be related and what is relation type and why? I think that only with CartItem. But I'm not sure. There can be many cart items in an order. So I guess relation should be one-to-many. Or maybe in cart item there should be Many-to-one relation with Order class, I'm not sure about it.
imp_o_rt
imp_o_rt3y ago
If you think about it, a cart sort of gets converted into an order. When you shop online you check out the cart which then becomes an order (with an id, line items etc). At that point the cart ceases to exist and you now have an order instead
Tomasm21
Tomasm21OP3y ago
So it's different class then which has additional fields like its own id, date, status( enum which can be - submitted, approved, withdrawn) and all info from CartItem - products, user info. Is it additional class OrderDetails? Or in the same order class? Then Order should not have any link to cart. But CartItemTransferDTO has all required data to make and Order
imp_o_rt
imp_o_rt3y ago
Well you can sort of make the order the cart If it's checked out it's an order. If not, cart When it's checked out you can add extra info (method of payment, checkout time etc)
Tomasm21
Tomasm21OP3y ago
And then CartItemDAO entries for this user deleted.
imp_o_rt
imp_o_rt3y ago
Well again you can just have an order item
Tomasm21
Tomasm21OP3y ago
User then sees not cart info, but an order which looks different than Admin or manager What is an order item? A class or a property(field) in CartItem?
imp_o_rt
imp_o_rt3y ago
An order has many order items (aka line items) The cart contents essentially The product, quantity, maybe other stuff? Can't think of any other stuff
Tomasm21
Tomasm21OP3y ago
So order is a different class entity right?
imp_o_rt
imp_o_rt3y ago
Yeah.
Order
- items: List<OrderItem>
- checkedOut: boolean
Order
- items: List<OrderItem>
- checkedOut: boolean
Some other stuff?
OrderItem
- product: Product
- quantity: int
OrderItem
- product: Product
- quantity: int
Tomasm21
Tomasm21OP3y ago
items: List<CartItem>
imp_o_rt
imp_o_rt3y ago
That's ok too That's how I'd do it anyway It's got some caveats eg making sure only one non-checked-out order per user (which would be their cart)
Tomasm21
Tomasm21OP3y ago
alright. thanks for info checking message so the post would not be locked...
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Tomasm21
Tomasm21OP3y ago
@Andrew Something is not right with my order that is created out of user's cart. The problem is that the database cannot create correct foreign key between Orders ID and Order Items entity field. The column ORDERS_ID at Item entity becomes null. And I think that it should be equal to the Order id for which those items belong to. Look at the picture of the database:
Tomasm21
Tomasm21OP3y ago
No description
Tomasm21
Tomasm21OP3y ago
Something is not right. Maybe I defined entities properties JPA relations wrong. Check it out:
@Entity
public class Orders {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@NotEmpty
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime submitedAt;

@NotEmpty
private String orderName;

@NotEmpty
@Column(name="`User`")
private String username;

@Enumerated(EnumType.STRING)
@Column
private OrderStatus status;

@OneToMany(mappedBy = "orders", cascade = { CascadeType.ALL}, fetch = FetchType.LAZY)
private Set<Item> items;
@Entity
public class Orders {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@NotEmpty
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime submitedAt;

@NotEmpty
private String orderName;

@NotEmpty
@Column(name="`User`")
private String username;

@Enumerated(EnumType.STRING)
@Column
private OrderStatus status;

@OneToMany(mappedBy = "orders", cascade = { CascadeType.ALL}, fetch = FetchType.LAZY)
private Set<Item> items;
And the Item:
@Entity
public class Item {

@Id
private Integer id;

@Column(name="`NAME`")
private String dishName;

@Column(name = "`DESCRIPTION`", length = 2000)
private String dishDescription;

@Column(name = "`QUANTITY`")
private Integer quantityInCart;

@Column(name = "`USER`")
private String username;

@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH })
@JoinColumn(name = "ORDERS_ID")
private Orders orders;
@Entity
public class Item {

@Id
private Integer id;

@Column(name="`NAME`")
private String dishName;

@Column(name = "`DESCRIPTION`", length = 2000)
private String dishDescription;

@Column(name = "`QUANTITY`")
private Integer quantityInCart;

@Column(name = "`USER`")
private String username;

@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH })
@JoinColumn(name = "ORDERS_ID")
private Orders orders;
How to do entities relation correctly? Should it be one direction or two-directional relationship? What are differences of these relations? And what kind of relationship I should use? Why? I was doing JUnit tests for the Orders service methods. It turns out that it can create orders. And Order items from user's cart. But when it is time to show order (GetMapping) then it returns Orders entity with empty items set. Obviously it happens because JPA cannot find foreign key of items field for its designated order. It is null. Can you help me to solve this thing?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Tomasm21
Tomasm21OP3y ago
I provided you with all the necessary information to realize the problem. It only looks too large.
JavaBot
JavaBot3y ago
Post Closed
This post has been closed by <@312509109863710732>.

Did you find this page helpful?