У меня есть некоторые проблемы с архитектурой базы данных. Я делаю простой интернет-магазин самостоятельно. Я не знаю, как сделать логическую базу данных. меня Пользователь, Корзина, CartItem, Pruduct и Order таблицы.Простая схема базы данных для магазина
Мои отношения:
пользователя OneToOne Корзина
Корзина OneToMany CartItem
CartItem ManyToOne Продукт
И я не знаю, соотношение которых я должен выбрать для таблицы Порядок. Пользователь может добавить любое количество товаров в свою корзину, и после подтверждения покупки я хочу сохранить результат в таблице заказов. Пожалуйста, помогите мне разобраться в этом. Я делаю интернет-магазин в первый раз, и это сложно для меня.
С наилучшими пожеланиями. Корзина
Entity
public class Cart {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToMany(mappedBy = "cart", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<CartItem> cartItems;
@OneToOne
private User user;
private Double grandTotal;
public Cart(List<CartItem> cartItems) {
this.cartItems = cartItems;
}
public Cart(Double grandTotal) {
this.grandTotal = grandTotal;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<CartItem> getCartItemsl() {
return cartItems;
}
public void setCartItemsl(List<CartItem> cartItemsl) {
this.cartItems = cartItemsl;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Double getGrandTotal() {
return grandTotal;
}
public void setGrandTotal(Double grandTotal) {
this.grandTotal = grandTotal;
}
}
CartItem
@Entity
public class CartItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne
@JsonIgnore
private Cart cart;
@ManyToOne
private Product product;
private Integer quantity;
private Double totalPrice;
public CartItem() {
}
public CartItem(Integer quantity, Double totalPrice) {
this.quantity = quantity;
this.totalPrice = totalPrice;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Cart getCart() {
return cart;
}
public void setCart(Cart cart) {
this.cart = cart;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public Double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(Double totalPrice) {
this.totalPrice = totalPrice;
}
}
Продукт
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String description;
private Float price;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Category category;
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<CartItem> cartItemList;
private boolean available;
public Product() {
}
public Product(String name, String description, Float price, Category category, boolean available) {
this.name = name;
this.description = description;
this.price = price;
this.category = category;
this.available = available;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public List<CartItem> getCartItemList() {
return cartItemList;
}
public void setCartItemList(List<CartItem> cartItemList) {
this.cartItemList = cartItemList;
}
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
}
В чем разница между товаром и карточкой? для меня они кажутся, что они должны быть одинаковыми – niceman
@niceman, только потому, что продукт существует (т. е. имеет запись «Продукт»), не означает, что он находится в корзине (т. е. имеет запись «CartItem»). –
Хммм Я вижу, пропустил это @JonathanM – niceman