I recently ran into this problem. I have a product that has a list of values in relation to volume. Example: enter image description here Entities :
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<>();
}
Controller :
@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";
}
The problem is that if I want to sort a product by its cost, I will be given duplicate objects that have several values. Example - http://localhost:8080/?sort=price.valume,ASC
How can I implement a request that will issue products for non-duplicates with different prices. For example: http://localhost:8080/?sort=price[0].valume,ASC