JPQL Query - SQL

0

Good evening.

I have a bean, which has an init () method. One of the goals of this init is to bring the cycle that was opened according to the course of that user, ie If there is no cycle or evaluation process involved with the course of that logged in user, no simulation will be shown for it to respond. p>

The problem is that you're making a dent in my Query:

private List<Ciclo> ciclosCurso = new ArrayList<Ciclo>();

private void init(){
  this.ciclosCurso = CicloDAO.buscaCicloPorCursoDoUsuario(codigoUsuario);
}

CICLODAO:

@SuppressWarnings("unchecked")
    public List<Ciclo> buscaCicloPorCursoDoUsuario(Long codigo) {
        return  manager
                .createQuery("select c from Ciclo c JOIN curso_ciclo a WHERE CODIGO_CURSO = ?1")
                .setParameter(1, codigo).getResultList();
    }

This table course_cycle is a relationship between Course and Cycle from many to many, ie @ManyToMany

It gives an error in Query,

ERROR: Path expected for join! Oct 31, 2015 12:17:29 AM org.hibernate.hql.internal.ast.ErrorCounter reportError ERROR: Path expected for join!

    
asked by anonymous 31.10.2015 / 03:27

1 answer

2

You are trying to write a sql query in a JPQL, in fact, navigation in a JPQL works like the navigation in your class itself, no tables involved, so to make your query the correct one would be.

"SELECT DISTINCT ciclo from Curso curso JOIN FETCH curso.ciclos ciclo WHERE curso.codigo = ?1"

This query may not work, as I'm just guessing the names of variables in your bean, but just replace.

The logic is simple, you are looking for the cycles present in the course bean where the course has the code equal to a certain value.

Another way to write the same query would be on the reverse side, without FETCH:

"SELECT ciclo from Ciclo ciclo WHERE ciclo.curso.codigo = ?1"

In this query, join is implicit by JPA, since the cycle has a course already assigned.

I particularly prefer the latter, simpler, clearer, higher performance, but you can choose the one that best suits you depending on your mapping

    
03.11.2015 / 11:10