Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

hibernate - HQL: illegal attempt to dereference collection

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

chapters is a collection in books and so will not hold the property title (Collection.title). You need to join the chapters in order to include them in your query like your first example. If your chapters are mapped lazily you will only get a collection of Book's without the chapters loaded in them. So I would say, use your first query.

For further reading, have a look at the query HQL joins and performance fetching pages.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...