Kiru
Kiru

Reputation: 169

How to implicitly join across collections in Hibernate?

The object structure is like below-

Entity A
  - Collection <B>

Entity B
  - Collection <C>

Entity C
  -Collection <D>

Entity D
  CompositePrimaryKey

Class CompositePrimaryKey
  String id;

In the HQL, the query is like from A a where a.B.C.D.CompositePrimaryKey.id = 'input';

I am getting the below exception-

org.hibernate.QueryException: illegal attempt to dereference collection

Basically this is implicit join across the collections. What is the right approach to do this?

I want to fetch all data through the collections - based on the id value

Thank you.

Upvotes: 1

Views: 2798

Answers (2)

Pascal Thivent
Pascal Thivent

Reputation: 570325

I am getting the (...) exception org.hibernate.QueryException: illegal attempt to dereference collection

The path expression a.B.C.D is illegal as B (and C and D) is a collection.

Basically this is implicit join across the collections. What is the right approach to do this?

Use an explicit join:

from A a join a.B b join b.C c join c.D d where d.pk.id = :id

Upvotes: 1

dma_k
dma_k

Reputation: 10639

To my knowledge HQL cannot dereference this complicated case. You need to specify the mapping for child collections as fetch="join" and write a big HQL join manually:

from A_table a, B_table b, C_table c, D_table d
  where a.b_id = b.id and b.c_id = c.id and c.d_id = d.id and d.id = 'input'

A good solution will also be to define the filter for entity A (filters operate with table columns):

<class name="A" ...>
    ...
    <filter name="id_filter" condition="D_table.id = :id" />
</class>

Upvotes: 1

Related Questions