I have the following query in JPQL
FROM User u INNER JOIN FETCH u.enderecos e WHERE u.id =:id ORDER BY e.id DESC
A user can have more than ten addresses, so we want to bring only ten addresses and if the user wants, he loads ten times.
I tried using setMaxResults (int) but not I get success, because as it returns only one user I believe that JPQL understands that it did its work, even though when I get the mirrored query in the console and throw it in my database it returns me more than one line.
Is it possible to do what I need? If so, how?
EDITION: Mapping as a request
public class Usuario implements Serializable {
private static final long serialVersionUID = 4769740907794027841L;
// Outros atributos
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "usuario")
private Set<Endereco> enderecos;
// Sets e gets
}
public class Endereco implements Serializable {
private static final long serialVersionUID = -6380840893466300379L;
// Outros atributos
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CD_USUARIO", nullable = false, referencedColumnName = "CD_USUARIO")
private Usuario usuario;
// Sets e gets
}
Also, I have the sql that brings me What I want, but I want, if possible, in JPQL to avoid using non-portable code like the rownum of oracle .
SELECT *
FROM user u INNER JOIN endereco e ON u.id_user = e.id_user
WHERE u.id_user = '123' AND ROWNUM = 1
ORDER BY e.id_address desc;