• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Querying: restricting collection based on parent?

Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a database with tables contact and address I would like to find the housemates of a given contact.
The following HQL query is a good start:

That is, it yields a collection of contacts, each of which has a single address, each of which has a collection of all contacts living at that address. As expected, each contact lists itself as housemate, which is not what I want. Let’s try a refinement:

Here, the ‘where’ clause is ignored, as per the Hibernate documentation (Section 14.3):

   A fetch join does not usually need to assign an alias, because the associated objects should not be used in the where clause (or any other clause). The associated objects are also not returned directly in the query results. Instead, they may be accessed via the parent object. The only reason you might need an alias is if you are recursively join fetching a further collection

The generated SQL works as expected, though.

Is there any way to get this to work, other than by manually populating the housemates collection?
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!