How to turn this query into a JPQL?

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 does it transform it into a JPQL query? I'm having a hard time returning values:

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

My two entities Employee and Project saw the tables:
Project
Employee
Employee_Project (employee_matricula, code_projects)

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?

    
asked by anonymous 05.04.2016 / 03:33

1 answer

1

In HQL / JPQL Relational object queries, the query is done based on the Classes and not on the database tables, without the name of the Classes / Fields it is difficult to pass you with exactness, however 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, also add the clause of where you have it.

    
05.04.2016 / 13:09