-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
I believe what you seek is Namedqueries, you configure the annotation for the entity.
– Wakim
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?
– João Neto
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.– Wakim