How to "set" a Query in the @Entity annotation with Hibernate?

-1

I have the following Class:

Student.java

@Entity
public class Aluno implements Serializable {

    @Id
    @Column(length=11, nullable=false)
    private int cpf;

    @Column(nullable=false)
    private String nome;

    @Column(nullable=false)
    private int rg;

    @Column(length=11,nullable=false)
    private int matricula;

    @Temporal(TemporalType.DATE)
    private Date dataNascimento;

    /* RELACIONAMENTOS */

    @OneToMany(mappedBy="aluno")
    private List<Inscricao> inscricoes;

    @OneToMany(mappedBy="aluno")
    private List<Relatorio> relatorios;

    /* GETTERS AND SETTERS */
    ...
}

It turns out that the attributes are in several tables of the database, so I created the Query, and I went to the Institution's DBAs to create the Views, which would make it a lot easier, but they are not working these days and I have to finish it until Monday , so an alternative was to put the querys in the code, I remember once I saw a code where the query was next to the @Entity annotation, could anyone help? How do I set this up in the code?

Follow my Query:

SELECT p.nome_pessoa, a.dt_nascimento, alc.matricula, dj.numero_documento as rg, df.numero_documento as cpf 
    FROM pessoas AS p
        INNER JOIN alunos AS a ON p.id_pessoa = a.id_pessoa
        INNER JOIN acad_alunos_cursos AS alc ON alc.id_aluno = a.id_aluno
        LEFT JOIN doc_pessoas AS df ON p.id_pessoa = df.id_pessoa AND df.id_tdoc_pessoa = 1
        LEFT JOIN doc_pessoas AS dj ON p.id_pessoa = dj.id_pessoa AND dj.id_tdoc_pessoa = 3
    
asked by anonymous 03.01.2015 / 17:30

1 answer

1

The annotation you need is a @NamedQueries. Add it above the class declaration as follows:

@Entity
@NamedQueries({
    @NamedQuery(name = "Aluno.NOME_DA_QUERY", query = "SELECT p.nome_pessoa, a.dt_nascimento, alc.matricula, dj.numero_documento as rg, df.numero_documento as cpf 
FROM pessoas AS p
    INNER JOIN alunos AS a ON p.id_pessoa = a.id_pessoa
    INNER JOIN acad_alunos_cursos AS alc ON alc.id_aluno = a.id_aluno
    LEFT JOIN doc_pessoas AS df ON p.id_pessoa = df.id_pessoa AND df.id_tdoc_pessoa = 1
    LEFT JOIN doc_pessoas AS dj ON p.id_pessoa = dj.id_pessoa AND dj.id_tdoc_pessoa = 3")})
    
03.01.2015 / 17:52