0

I have an entity which is associated to another via a One-to-Many relationship.

When i load the entity i want to load only the size of the associated collection(how many entities is this one associated with) & i want to do it in an EAGER way so that this is fetched at the time the base entity is fetched(in a single Query).

Hibernate count collection size without initializing shows how to do it in a Lazy way . I want to do it in eager way since i have some UI feature based on this and don't want to query multiple times.

Could use Formula but they are not HQL supporting.Any other way in hibernate?

Community
  • 1
  • 1
nikel
  • 2,856
  • 8
  • 34
  • 67

2 Answers2

0

Hibernate has an annotation called Formula that should work for you:

http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e2785

  • 1
    as i mentioned in question - Formulas are not HQL supporting. I would like to avoid that if possible. – nikel Feb 12 '16 at 03:03
0

You can use a HQL query for this purpose:

select e, (select count(c) from Entity e1 join e1.collection c where e1.id = :id)
from Entity e
where e.id = :id
Dragan Bozanovic
  • 21,631
  • 4
  • 36
  • 100
  • I want to put this data into the entity class and auto fetch it while it gets loaded so this logic stays in a single place. how do i do that with above? – nikel Feb 12 '16 at 13:20