# Java SDK Query Predicate builders Create readable and maintainable Query Predicates using the Java SDK. The [Java SDK Query Predicate builder](https://github.com/commercetools/commercetools-sdk-java-v2/tree/main/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/predicates) offers a fluent, type-safe interface for building [Query Predicates](/api/predicates/query.md). Alternatively, you can use query predicates directly as strings with [input variables](/api/predicates/query.md#input-variable-examples). Input variables allow you to parameterize predicates and avoid string concatenation: ```java title="Use input variables with Query Predicates" // Using an input variable for a single value CustomerPagedQueryResponse response = httpApiRoot .customers() .get() .withPredicateVar("name", "John") .withWhere("firstName = :name") .executeBlocking() .getBody(); // Using an input variable for an array of values CartPagedQueryResponse cartResponse = httpApiRoot .carts() .get() .addPredicateVar("statuses", Arrays.asList("Open", "Merged")) .withWhere("cartState in :statuses") .executeBlocking() .getBody(); ``` ## Example usage The following examples demonstrate how to use the Query Predicate builder for different scenarios. More examples can be found [here](https://github.com/commercetools/commercetools-sdk-java-v2/tree/main/commercetools/commercetools-sdk-java-api/src/test/java/com/commercetools/api/predicates/query). ```java title="Build a Query Predicate for Customers" import static com.commercetools.api.predicates.query.CustomerQueryBuilderDsl.*; // Finds Customers called "John Doe" String predicate = CustomerQueryBuilderDsl.of() .firstName().is("John") .and() .lastName().is("Doe") .build(); ``` ### Equality comparisons ```java title="Equality comparisons" // Finds a Cart where the `id` is "abc" CartQueryBuilderDsl.of().id().is("abc"); // Finds all Customers born on 12 October 2005 CustomerQueryBuilderDsl.of().dateOfBirth().is(LocalDate.parse("2005-10-12")); // Finds all Tax Rates with an `amount` of 19% TaxRateQueryBuilderDsl.of().amount().is(0.19); ``` ### Logical operators ```java title="Logical operators" // Finds Carts where the `id` is not "abc" CartQueryBuilderDsl.of().id().is("abc").not(); // Finds Carts where the `id` is "abc" and `customerId` is "def" CartQueryBuilderDsl.of().id().is("abc").and(p -> p.customerId().is("def")); // Finds Carts where the `id` is "abc" or `customerId` is "def" CartQueryBuilderDsl.of().id().is("abc").or(p -> p.customerId().is("def")); ``` ### Range comparisons ```java title="Range comparisons" // Finds Carts where `version` < 10 CartQueryBuilderDsl.of().version().isLessThan(10L); // Finds Carts where `version` > 10 CartQueryBuilderDsl.of().version().isGreaterThan(10L); // Finds all Tax Rates with an `amount` of less than 19% TaxRateQueryBuilderDsl.of().amount().isLessThan(0.19); // Finds all Customers born after 12 October 2005 CustomerQueryBuilderDsl.of().dateOfBirth().isGreaterThan(LocalDate.parse("2005-10-12")); ``` ### Collection queries ```java title="Collection queries" // Finds Carts where `key` is `foo` or `bar` CartQueryBuilderDsl.of().key().isIn(Arrays.asList("foo", "bar")); // Finds Tax Rates where the amount is 19% or 20% TaxRateQueryBuilderDsl.of().amount().isIn(Arrays.asList(0.19, 0.20)); ``` ### Nested queries ```java title="Nested queries" // Finds a Product where the current projection's English slug is "super-product" and the English name is "Super Product" ProductQueryBuilderDsl.of() .masterData(m -> m.current(c -> c.slug(l -> l.with(Locale.ENGLISH).is("super-product")) .and(t -> t.name(l -> l.with(Locale.ENGLISH).is("Super Product"))))); ``` ### Special cases ```java title="Special cases" // Finds Carts which have a `key` CartQueryBuilderDsl.of().key().isDefined(); // Finds Carts which do not have a `key` CartQueryBuilderDsl.of().key().isNotDefined(); // Finds Carts without Line Items CartQueryBuilderDsl.of().lineItems().isEmpty(); // Finds Types where the `resourceTypeIds` contains `approval-flow` TypeQueryBuilderDsl.of().resourceTypeIds().containsAnyVar("approval-flow"); ``` ## Related pages - [Area overview page with navigation](/dev-tooling.md) - [Previous page: Best practices](/dev-tooling/java-sdk-best-practices.md)