Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP 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 ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

sub query in HQL

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

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 Rule.id not in (select rule.id from Owner as o join Rule as rule)

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

 
Cokie Burun
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 rl.id not in (select o.Rules.id from Onwer o)]
at org.hibernate.hql.ast.tree.DotNode$1.buildIllegalCollectionDereferenceException(DotNode.java:69)
at org.hibernate.hql.ast.tree.DotNode.checkLhsIsNotCollection(DotNode.java:536)
at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:244)
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:117)
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:113)
at org.hibernate.hql.ast.tree.DotNode.resolveSelectExpression(DotNode.java:649)
at org.hibernate.hql.ast.HqlSqlWalker.resolveSelectExpression(HqlSqlWalker.java:761)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:1885)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1825)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1394)
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


John
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic