[英]Maintain an order with a OneToMany join
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? 我正在使用Spring Boot,并想将产品列表作为外键添加到页面上,但是从数据库中检索时如何保持ArrayList顺序? Should I have an intermediate table eg
PageProductOrder
which maintains product primary key and order column? 我是否应该有一个中间表(例如
PageProductOrder
来维护产品主键和订单列?
@Entity
public class Page {
@OneToMany
@JoinColumn(name = "product_id")
private List<Product> products;
You could do this by using @OrderColumn
- this will use a column in the entity for ordering : 您可以使用
@OrderColumn
进行此@OrderColumn
-这将使用实体中的列进行排序:
@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. Product
实体中的product_index
列将用于维护订单。
You can define the order by using the @OrderBy
annotation. 您可以使用
@OrderBy
批注定义订单。
@OneToMany
@JoinColumn(name = "product_id")
@OrderBy(value = "name ASC")
private List<Product> products;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.