4
I have the following entity Cliente
who owns Pedidos
.
The relationship between customers and orders is mapped with Cliente @OneToMany
and Pedidos @ManyToOne
. What I need is for the Customer Order list to be sorted by Pedido.id
.
After some research I discovered that I should use @OrderBy
, the problem is that I am not knowing how to use it. Follow 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;
That said, as my note should be @OrderBy
so that when I pull the customers, the orders come ordered by id
?
Follow the code of how I 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 consulting clients, the framework already returns me the list of Client with the appropriate joins in Orders...
How are you browsing these logs? Are you using Criteria, JPQL? I’m not a fan of using
@OrderBy
to address problems that may be specific.– Dherik
I am using Entitymanager public class Clientepu Implements Serializable{ @Inject private Entitymanager manager; public Client clientPorEmail(String email){ Return manager.find(Client.class, email); }
– Murilo Medeiros
edit your question and place the code above there in question :). It is more readable this way and this part is very important in your question
– Dherik
Follow the code edited above
– Murilo Medeiros
@Murilomedeiros the solution must be at all times in the area of answers. Since there is already an answer that solves the problem, you don’t need to do anything else - just the fact that you accepted the answer already indicates that it has solved your problem.
– Woss