tags:

views:

30

answers:

2

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.

+1  A: 

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>
dma_k
I used the filter logic and it works fine for my requirement, thanks.
Kiru
A: 

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
Pascal Thivent