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

Asked

Viewed 308 times

-1

I have the following class:

Java student.

@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 Dbas of the Institution to create the Views, which would make it a lot easier, only they are not working these days and I need to finish this by Monday, then an alternative was to put the querys in the code, remember that once I saw a code where the query was next to the @Entity annotation, could anyone help? How do I set this up in 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
  • 1

    I believe what you seek is Namedqueries, you configure the annotation for the entity.

  • I’m trying the touchmx answer, but how to make sure that the tables I have in the query (Inner Join, etc.) do not have entities?

  • Named queries are JPQL. I suspect that for your case it is best to create a class that stores these native Queries in static attributes and use the EntityManager.createNativeQuery. The drawback is that if there are errors, they will be detected at runtime, unlike the Namedqueries that are checked at Deployment time.

1 answer

1


The annotation you need is a @Namedqueries. Add it above the class statement 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")})
  • The named query then will be in sql? Doesn’t it need to be in hql? I’ll try here.

  • Gave the following error: people is not Mapped [SELECT p.person AS name, a.dt_birth AS dataNascimento...]

  • Use for person, students and the like (representing tables) the name of the objects that reference them, In case if people is an object put the name of the person class (which must be People, in capital letters)

  • I don’t have a class Person, I just have the student class. Ai student is formed by attributes of classes: Students , People, acad_alunos_courses and doc_people

Browser other questions tagged

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