Search code examples
hibernatehqlhibernate-onetomany

hibernate on to many reverse query


I have many2one relation between 2 entities Question and Answer with many2one defined in Answer' and no one2many defined inQuestion`.

How can i query with hibernate the questions that do not have any answers without adding one2many relation in Question entity?

something like:

select distinct q from Question q
left join Answer a on a.question_id=q.id
where a.id is null

Solution

  • Something like this:

    select q from Question q
    where q not in (select a.question from Answer a)
    

    Btw, your supplied query is "too much SQL", remember that although their syntax are similar, SQL and JPQL are conceptually different.