Answer the question
In order to leave comments, you need to log in
What relationship should be between these two Entities?
There are 3 Entity
Order, OrderItem, Product
@Entity
public class Order {
lic class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@OneToMany(
mappedBy = "order",
cascade = CascadeType.ALL,
orphanRemoval = true)
private List<OrderItem> orderItems;
}
@Entity
public class OrderItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private Product product;
private int quantity;
@ManyToOne
private Order order;
}
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String description;
private Category category;
}
Answer the question
In order to leave comments, you need to log in
Something similar has already been discussed in this question:
https://qna.habr.com/q/1111518
Let's say that you have a Product entity, as well as an Order entity.
In one order, there may be a certain quantity of one product, which means that you also need to store the quantity of ordered goods somewhere.
For this, aggregation is suitable, as an option.
Those. need to add extra. a table (for example, OrderData) in which foreign_keys will be both product_id and order_id, and will be. add. quantity column.
+----------+------------+----------+
| order_id | product_id | quantity |
+----------+------------+----------+
| 1 | 2 | 10 |
| 2 | 3 | 11 |
+----------+------------+----------+
Product {
long productId;
}
Order {
long orderId;
}
OrderData {
Order order;
Product product;
int quantity;
}
Product {
long productId;
}
Order {
@MapKeyColumn(name = "product_id")
@Column(name = "quantity")
Map<Long, Integer>
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question