How to turn this query into a JPQL?

Asked

Viewed 238 times

3

I have the following query in the postgres database:

select * from trabalha_projeto tp inner join Empregado e on
e.matricula = tp.empregado

how it turns it into a JPQL query?
I’m having a hard time returning the values:

  • First : for not knowing how to formulate the query with jpql
  • Second: Due to the fact of having two entities java, and to relate them have three tables in the database

My two entities Employee and Project saw the tables:
Project
Employee
Empregado_Projeto(empregado_matricula,projetos_codigo)

Here are my java classes:

Employee class

@Entity
@SequenceGenerator(name = "empregado_sequence", sequenceName = "empregado_sequence",
        allocationSize = 1, initialValue = 1)
public class Empregado implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "empregado_sequence")
    private int matricula;
    private String nome;
    private Double salario;
    @OneToMany(cascade = CascadeType.ALL, targetEntity = Projeto.class ,fetch = FetchType.EAGER)
    private List<Projeto> projetos;

    public Empregado() {
    }

Project class

@Entity
@SequenceGenerator(name = "projeto_sequence" , sequenceName = "projeto_sequence",
           allocationSize =  1 , initialValue = 1)
public class Projeto implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE , generator = "projeto_sequence")
    private int codigo;
    private String nome;

    public Projeto() {
    }

How to return all employees working on a project using a Join?

1 answer

1

In HQL/JPQL Relational object queries, the query is based on the Classes and not on the tables of the database, without the name of the Classes/Fields it is difficult to pass you with accuracy, but following the convention, your query would be something like this:

from Projeto p join p.empregado

If the query does not work, check the name of your Classes and the field of Join(employee), also add Where clause if you have it.

  • Thanks for the force, I edited the question to better clarify my doubt.

Browser other questions tagged

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