我最近遇到了这个问题。我有一个产品,其中包含与数量相关的值列表。示例:在此处输入图像描述 实体:
public class Price implements Serializable, Comparable<Price> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "product_id")
private Product product;
private int valume;
private int cost;
@Override
public int compareTo(Price o) {
if (this.valume > o.getValume()) {
return 1;
} else if (this.valume < o.getValume()) {
return -1;
} else {
return 0;
}
}
}
public class Product implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String description;
private LocalDateTime data;
@OneToMany(targetEntity = Price.class, mappedBy = "product",
cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
private List<Price> price = new ArrayList<>();
}
控制器 :
@GetMapping
public String tapePage(@PageableDefault(size = 12, sort = "data", direction = Sort.Direction.DESC) Pageable pageable,
Model model){
model.addAttribute("products", productService.getAllProducts(pageable));
return "tape";
}
问题是,如果我想按成本对产品进行排序,我会得到具有多个值的重复对象。示例 -http://localhost:8080/?sort=price.valume,ASC
我如何实现一个请求,该请求将为具有不同价格的非重复产品发出产品。例如:http://localhost:8080/?sort=price[0].valume,ASC