V
V
Valeriy_Morozov2022-02-12 01:19:12
Java
Valeriy_Morozov, 2022-02-12 01:19:12

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

OrderItem has only 2 fields and back reference to Order

@Entity
public class OrderItem {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private Product product;
    private int quantity;
 @ManyToOne
    private Order order;
}

In fact, I use it as a replacement for map to know how many times a product has been ordered. And here is Product

@Entity
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
 private long id;
    private String name;
    private String description;
    private Category category;
    }

If everything is clear with the relations Order - OrderItem ( OneToMany bidirectional );

Then what relationship should be between Product and OrderItem? If @ManyToMany, then there will be no problems with database calls and optimization if it is ManyToMany unidirectional ( OneToMany unidirectional has them, but I don’t know about ManyToMany ).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2022-02-12
@Valeriy_Morozov

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 |
+----------+------------+----------+

So you should end up with something like this (pseudocode):
Product {
long productId;
}
Order {
long orderId;
}
OrderData {
Order order;
Product product;
int quantity;
}

Another option:
it's as stated in the question in the link above. Use map.
Here is a link to a useful resource:
https://www.baeldung.com/hibernate-persisting-maps
What should happen in this case (pseudocode):
Product {
long productId;
}

Order {

@MapKeyColumn(name = "product_id")
@Column(name = "quantity")
Map<Long, Integer>
}

In this case, we are pointing to the MapKeyColumn. Those. unique the key will be the product ID. And the value will be its number (the name is indicated in the Column).
Surely, if you think more, you can find more options.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question