Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

sub query in HQL

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am having trouble with a HQL query. I have a Rules table, a Owner table and a Owner_Rules that holds one to many mapping between owner and their rules (one owner has many rules). The Rules table also has rules that do not have any owners.

I have a rules class, a owner class with one to many mapping for rules (collection).

I would like to write a HQL query that gives me all the rules without any owners.
Something like this pseudo query

where not in (select from Owner as o join Rule as rule)

But it doesn't work. Any help is greatly appreciated.

Cokie Burun
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the error

org.hibernate.QueryException: illegal attempt to dereference collection [Owner.AutoID.Rules] with element property reference [id] [from Rule as rl where not in (select from Onwer o)]
at org.hibernate.hql.ast.tree.DotNode$1.buildIllegalCollectionDereferenceException(
at org.hibernate.hql.ast.tree.DotNode.checkLhsIsNotCollection(
at org.hibernate.hql.ast.tree.DotNode.resolve(
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(
at org.hibernate.hql.ast.tree.DotNode.resolveSelectExpression(
at org.hibernate.hql.ast.HqlSqlWalker.resolveSelectExpression(
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cokie,

I do not know HQL, but JPAQL and I think they are very similar (or at least they should be ).

There is a special keyword: is empty.

Assuming you make your relation bidirectional you should be able to write something like

where rule.owner is empty

But having written this I think this may only work with toMany-relations...

What about

where rule.owner is null (or = null?).

if you want to use subqueries you can use not exists like in this example:

This example is from the Hibernate documentation: Hibernate Documentation in Chapter 14.13

In the renaissance, how big were the dinosaurs? Did you have tiny ads?
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic