• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HQL: illegal attempt to dereference collection

 
Rohit Bhal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The situation is like this:

I have an entity Book that holds a one-to-many relationship with Chapter.

Now if I try the query, "from Book book inner join book.chapters chapter where chapter.title like '%hibernate%'", it gives me the desired result.

But if I try, "from Book where book.chapters.title like '%hibernate%'", I get the error illegal attempt to dereference collection.

The thing is that I only want the collection of Book objects in return and not a collection of pair of Book and Chapter objects in return which I get with the former query.

Could someone help me understand?
 
Bogdan Baraila
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
You can also try with: "select distinct ch.book from Chapter ch where ch.title like '%hibernate%'"
 
joy b chakravarty
Ranch Hand
Posts: 62
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if I try, "from Book where book.chapters.title like '%hibernate%'", I get the error illegal attempt to dereference collection.


This would not work... as its something like book.getChapters().getTitle() .. as as book.getChapters() returns a collection, the getTitle fails.
 
Rohit Bhal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@joy: I think query like "...book.chapters.title..." is not supported anymore. I am not sure if it was supported in the older version of Hibernate either. Thanks for reply

@Bogdan: Yes, "select book from Book book inner join book.chapters chapter where chapter.title like '%Hibernate%'" gave me exactly that. Thanks for reply
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic