I have a scenario with the following entities: Treinamento
, Curso
and Aulas
.
A Treinamento
has a ManyToOne
relationship for Curso
.
And Aula
has a ManyToOne
relationship to Curso
.
I want to make a query that given a given training id returns me the training with your course and with their respective classes filled in the object. I'm doing the query as follows:
Treinamento retorno = null;
StringBuilder sb = new StringBuilder();
sb.append("SELECT t FROM Treinamento t ");
sb.append("INNER JOIN t.idCurso c ");
sb.append("LEFT JOIN FETCH c.aulas a ");
sb.append("WHERE t = '"+String.valueOf(treinamento)+"' ");
retorno = (Treinamento)this.em.createQuery(sb.toString()).getSingleResult();
And I'm getting the error:
Caused by: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=a,role=br.com.empresa.domain.ptreinamentos.Curso.aulas,tableName=Aula,tableAlias=aulas2_,origin=Curso curso1_,columns={curso1_.id ,className=br.com.empresa.domain.ptreinamentos.Aula}}]
The error tells me that the owner of the fetch relationship is not in the select clause. In this case the owner of the relationship would be Curso
. What would be the correct way to do this query?