Maintain an order with a OneToMany join

lennard :

I'm using spring boot and want to add a list of products to a page as a foreign key but how can I maintain the ArrayList order when I retrieve from the database? Should I have an intermediate table e.g. PageProductOrder which maintains product primary key and order column?

@Entity
public class Page {
    @OneToMany
    @JoinColumn(name = "product_id")
    private List<Product> products;
michalk :

You could do this by using @OrderColumn - this will use a column in the entity for ordering :

@OneToMany
@JoinColumn(name = "product_id")
@OrderColumn(name = "product_index")
private List<Product> products;

The column product_index in Product entity will be used for maintaining order.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=128004&siteId=1