Domain Queries Collection Expressions 2

Wolfgang Schuetzelhofer edited this page Jul 31, 2017 · 3 revisions
Previous Next Table of Contents

Domain Queries

Collection Expressions Continued

In this section you learn about two additional Collection Expressions: UNION and INTERSECTION.

UNION

A union expression produces a result set which contains all elements of two or more source sets.

// create a DomainQuery object
DomainQuery q = domainAccess.createQuery();
// create a DomainObjectMatch for objects of type Person
DomainObjectMatch<Person> smithsMatch = q.createMatch(Person.class);

// constrains the set of Persons to contain all Smiths
q.WHERE(smithsMatch.atttribute("lastName")).EQUALS("Smith");

// create a DomainObjectMatch for objects of type Person
DomainObjectMatch<Person> berghammersMatch = q.createMatch(Person.class);

// constrains the set of Persons to contain all Berghammers
q.WHERE(berghammersMatch.atttribute("lastName")).EQUALS("Berghammer");
		
// build the union, containing all Smiths and all Berghammers
// Note: you can build the union of any number of sets
DomainObjectMatch<Person> smiths_berghammersMatch =
   q.UNION(smithsMatch, berghammersMatch);
		
// execute the query
DomainQueryResult result = q.execute();
// retrieve the list of matching domain objects
List<Person> smiths_berghammers = result.resultOf(smiths_berghammersMatch);

INTERSECTION

An intersection expression produces a result set of elements which are contained in each of two or more source sets. Elements which are not contained in each of the source sets will not be part of the result set.
Imagine, you want to find all persons who have exactly two true siblings. True siblings have the same mother and father. The following sample shows how to solve that problem by using an intersection expression.

// create a DomainQuery object
DomainQuery q = domainAccess.createQuery();
		
// create a DomainObjectMatch for objects of type Person
// by default the set contains all persons
DomainObjectMatch<Person> personsMatch = q.createMatch(Person.class);
		
// retrieve a set of persons who have the same mother as a given person
DomainObjectMatch<Person> m_childrenMatch = q.TRAVERSE_FROM(personsMatch)
   .FORTH("mother")
   .BACK("mother").TO(Person.class);
// retrieve a set of persons who have the same father as a given person
DomainObjectMatch<Person> f_childrenMatch = q.TRAVERSE_FROM(personsMatch)
   .FORTH("father")
   .BACK("father").TO(Person.class);
// build a set of persons who have the same mother and father (true siblings)
DomainObjectMatch<Person> siblingsMatch =
   q.INTERSECTION(m_childrenMatch, f_childrenMatch);
		
// select all persons who have exactly two siblings
DomainObjectMatch<Person> have2SiblingsMatch =
   q.SELECT_FROM(personsMatch).ELEMENTS(
      q.WHERE(siblingsMatch.COUNT()).EQUALS(2)
   );
		
// execute the query
result = q.execute();
// retrieve the list of matching domain objects
List<Person> have2Siblings = result.resultOf(have2SiblingsMatch);

Previous Next Table of Contents