Getting Started
Whether you want to create a criteria subquery, or a projection subquery, you'll first need to get a new instance of the Detached Criteria Builder class. Since all of the subqueries we're creating are being added to our main criteria query either as a criteria or a projection, we can get the Detached Criteria Builder like so:
1
// Get a reference to a criteria builder from an ORM base or virtual service
2
c = ormservice.newCriteria();
3
4
// detached criteria builder
5
c.createSubcriteria( entityName='Car', alias='CarSub' );
Copied!
The arguments for the createSubcriteria() method are:
Argument
Type
Required
Default
Description
entityName
string
true
---
The name of the entity to bind this detached criteria builder with.
alias
string
true
---
The alias to use for the entity
Once the Detached Criteria Builder is defined, you can add projections, criterias, and associations, just like you would with a normal Criteria Builder.
Examples
1
c.newCriteria( entityName=‘Car’ );
2
3
// all-in-one criteria subquery
4
c.add(
5
c.createSubcriteria( ‘Car’, ‘CarSub’ )
6
.withProjections( property=’CarID’ )
7
.isEq( ‘Make’, ‘Ford’ )
8
.propertyIn( ‘CarID’ )
9
).list();
10
11
// detached criteria builder separately, then add as criteria
12
var dc = c.createSubcriteria( ‘Car’, ‘CarSub’ )
13
.withProjections( property=’CarID’ )
14
.isEq( ‘Make’, ‘Ford’ );
15
// add criteria subquery
16
c.add( dc.propertyIn( ‘CarID’ ) );
Copied!
Last modified 3yr ago
Copy link