JPQL - SQL query

Asked

Viewed 168 times

0

good night.

I have a bean, which has an init() method. One of the objectives of this init is to bring the cycle that was opened according to the course of that user, that is, If there is no cycle or evaluation process involved with the course of that logged in user will not be shown any simulated for him to answer.

The problem is that you are giving my Query:

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

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

CYCLODAO:

@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 course table is a relationship between Course and Cycle of many for many, ie, @Manytomany

Give a query error,

ERROR: Path expected for Join! Oct 31, 2015 12:17:29 AM org.hibernate.hql.internal.Ast.Errorcounter reportError ERROR: Path expected for Join!

  • Missing clause ON of JOIN

1 answer

2


You are trying to write an sql query in a JPQL, in fact, the navigation in a JPQL works like the navigation in your same class, 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 might not work right away as I’m just guessing the names of the variables in your bean, but just replace.

The logic is simple, you are searching 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 the other way around, without FETCH:

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

In this query, Join is implied 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 suits you best depending on your mapping

Browser other questions tagged

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