JPA ordered return from the Many side of an @Onetomany entity using @Orderby

Asked

Viewed 193 times

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...

  • 2

    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.

  • I am using Entitymanager public class Clientepu Implements Serializable{ @Inject private Entitymanager manager; public Client clientPorEmail(String email){ Return manager.find(Client.class, email); }

  • 1

    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

  • Follow the code edited above

  • 2

    @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.

1 answer

4


If I were you, I’d give up @OrderBy, because it would be applied in the requests for any query of the Customer entity. I recommend using JPQL to make this query:

public Cliente clientePorEmail(String email) {
    Query query = manager.createQuery("SELECT cliente FROM Cliente cliente
        JOIN cliente.pedidos pedido 
        WHERE cliente.email = :email ORDER BY pedido.id");
    query.setParameter("email", email);
    return (Cliente) query.getSingleResult();
}

If you really want to use the annotation, you can use it as follows if the goal is to sort by the order ID:

@OrderBy("id ASC")
List <Pedido> pedidos;
  • I’m sorry about the previous answer, I read the code differently... I don’t intend to use Query this way because it will result in the performance of the application... Using @Orderby would be a great option for me!

  • 1

    @Murilomedeiros, which paradigm is breaking? But look at my answer again, I put how to use the Orderby annotation.

  • 1

    Good ! solved! How do I mark the topic as solved?

  • @Murilomedeiros, just accept the answer (as already done) that in Sopt this already means that it has been solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.