I have the following Cliente
entity that has Pedidos
.
The relationship between clients and requests is mapped with Cliente @OneToMany
and Pedidos @ManyToOne
. What I need is for the Customer Requests list to be sorted by Pedido.id
.
After some research I discovered that I should use @OrderBy
, the problem is that I'm not sure how to use it. Here is the code:
@Entity
@Table
public class Cliente implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Email
@NotEmpty
private String email;
@OneToMany(mappedBy ="cliente",cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@OrderBy("cliente.Pedido.id ASC")
private List<Pedido> pedidos;
@Entity
@Table
public class Pedido implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_pedido")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private Cliente cliente;
Given this, how should my @OrderBy
annotation be so that when I pull out the clients, the requests are sorted by id
?
Here is the code for how to query the data:
public class ClientePU implements Serializable{
@Inject
private EntityManager manager;
public Cliente clientePorEmail(String email){
return manager.find(Cliente.class, email);
}
public void salvarNovoCliente(Cliente cliente){
manager.merge(cliente);
}
}
When querying clients, the framework already returns me the list of Customers with the proper joins in Orders ...