83

I am trying following hql query to execute

SELECT count(*) 
  FROM BillDetails as bd
 WHERE bd.billProductSet.product.id = 1002
   AND bd.client.id                 = 1

But it is showing

org.hibernate.QueryException: illegal attempt to dereference collection 
[billdetail0_.bill_no.billProductSet] with element property reference [product] 
[select count(*) from iland.hbm.BillDetails as bd where bd.billProductSet.product.id=1001 and bd.client.id=1]
    at org.hibernate.hql.ast.tree.DotNode$1.buildIllegalCollectionDereferenceException(DotNode.java:68)
    at org.hibernate.hql.ast.tree.DotNode.checkLhsIsNotCollection(DotNode.java:558)
1
  • Can you add your classes' definitions? Do they contain relationship definitions in them? Commented Jan 24, 2016 at 8:43

2 Answers 2

171

billProductSet is a Collection. As such, it does not have an attribute named product.

Product is an attribute of the elements of this Collection.

You can fix the issue by joining the collection instead of dereferencing it:

SELECT count(*) 
  FROM BillDetails        bd 
  JOIN bd.billProductSet  bps 
 WHERE bd.client.id       = 1
   AND bps.product.id     = 1002
Sign up to request clarification or add additional context in comments.

5 Comments

It doesn't work in my testing, if billProductSet is got be @JoinTable, and the relation is ManyToMany.
@Stony It does work with @JoinTable and @ManyToMany. I've that running right now.
Just for reference: in my case I was already doing the join of the collection but without giving it an alias it won't work. Thanks!
Only JOIN isn't worked for me. I used LEFT JOIN for my query.
your answer saved me a lot of time. Thanks.
1

because billProduct is one to many mapping and there is many billProduct entity from one BillDetails entity you can't dereference it in query.you must join BillDetails model to billProduct and filter result with where cluase.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.