10.2. Relation Traversal

Relations between objects can be traversed using Java-like syntax. For example, if the Magazine class has a field named "publisher" or type Company, that relation can be queried as follows:

select x from Magazine x where x.publisher.name = 'Random House'

This query returns all Magazine instances whose publisher field is set to a Company instance whose name is "Random House".

Single-valued relation traversal implies that the relation is not null. In SQL terms, this is known as an inner join. If you want to also include relations that are null, you can specify:

select x from Magazine x where x.publisher.name = 'Random House' or x.publisher is null

You can also traverse collection fields in queries, but you must declare each traversal in the from clause. Consider:

select x from Magazine x, in(x.articles) y where y.authorName = 'John Doe'

This query says that for each Magazine x, traverse the articles relation and check each Article y, and pass the filter if y's authorName field is equal to "John Doe". In short, this query will return all magazines that have any articles written by John Doe.

[Note]Note

The in() syntax can also be expressed with the keywords inner join. The statements select x from Magazine x, in(x.articles) y where y.authorName = 'John Doe' and select x from Magazine x inner join x.articles y where y.authorName = 'John Doe' are synonymous.

 

Skip navigation bar   Back to Top