> For the complete documentation index, see [llms.txt](https://coldbox-orm.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coldbox-orm.ortusbooks.com/v5.x/criteria-queries/criteria-builder.md).

# Criteria Builder

Build ORM queries fluently with newCriteria(), the bx-orm criteria builder

The criteria builder lets you build a query one method at a time instead of writing an HQL string. You chain conditions, joins, projections and ordering, then run it with a terminal method such as `list()`, `count()` or `get()`. Property names are checked as you add them and every value is bound as a query parameter, so there is no string concatenation and no SQL injection risk.

In cborm 6, `newCriteria()` returns the criteria builder of the [bx-orm](https://forgebox.io/view/bx-orm) module: the same object BoxLang's `entityCriteria()` function returns. It keeps the cborm method names you already know (`isEq`, `like`, `between`, `isIn`, `joinTo`, `withProjections`, `list`, `count`, `get`, `getOrFail`, ...) and adds many more (`paginate()`, `pluck()`, `each()`, `chunk()`, `updateAll()`, `deleteAll()`, `lock()`, function paths, ...).

```javascript
// Active users named Luis
userService
    .newCriteria()
    .isEq( "firstName", "Luis" )
    .isTrue( "isActive" )
    .getOrFail();

// Active admins, as a Java stream
userService
    .newCriteria()
    .isTrue( "isActive" )
    .isEq( "role.name", "admin" )
    .asStream()
    .list();

// Only a few columns, as an array of structs
userService
    .newCriteria()
    .withProjections( property = "id,firstName:fname,lastName:lname,age" )
    .isTrue( "isActive" )
    .joinTo( "role", "r" )
    .isEq( "r.name", "admin" )
    .asStruct()
    .list();
```

## How it works

* **Building methods** (conditions, joins, ordering, options) change the criteria and return it, so they chain.
* **Terminal methods** (`list()`, `count()`, `get()`, `paginate()`, ...) run one query and return its result. They never change the criteria, so the same criteria can run `count()` and then `list()`, in any order.
* `copy()` returns an independent copy to branch from.
* Method names are case-insensitive, and arguments can be positional or named: `isEq( "lastName", "Majano" )` or `isEq( property = "lastName", value = "Majano" )`.
* Property names are case-insensitive too: the builder resolves each path against the entity's mapping and fixes its casing. A misspelled property fails at once with a suggestion, for example `User has no property [fristName]. Did you mean [firstName]?`

{% hint style="success" %}
**Tip**: You don't have to use the ORM for everything. Please be pragmatic. If you can't figure a query out in 10 minutes or less, move to HQL or direct SQL.
{% endhint %}

## Changes from cborm 5

{% hint style="info" %}
Hibernate 7 removed the legacy Criteria API (`org.hibernate.Criteria`, `Restrictions`, `DetachedCriteria`, `Projections`, `org.hibernate.criterion.*`), so cborm 5's `CriteriaBuilder` and `DetachedCriteriaBuilder` wrappers are gone. `newCriteria()` now returns the bx-orm builder, which compiles to HQL. Most cborm code keeps working unchanged. The main differences:

* `get( properties )` becomes `withProjections( property = "..." ).asStruct().get()`.
* `createSubcriteria()` and the Detached Criteria Builder become `c.subquery( "Entity", "alias" )`: see [Subqueries](/v5.x/criteria-queries/detached-criteria-builder.md).
* `cacheRegion( name )` becomes `cache( true, name )`.
* `asStream()` returns a Java stream instead of a cbStreams stream.
* `idCast()`, `autoCast()` and typed SQL parameters are no longer needed: bx-orm converts values itself.
* `getNativeCriteria()`, `resultTransformer()`, `setProjection()`, `c.projections`, SQL projections and `getPositionalSQLParameters()` no longer exist.

Adobe ColdFusion and Lucee are not supported by cborm 6: stay on the cborm 5.x series for those engines.
{% endhint %}

## Where to go next

* [Getting Started](/v5.x/criteria-queries/criteria-builder/getting-started.md): get a criteria and run your first queries.
* [Restrictions](/v5.x/criteria-queries/criteria-builder/restrictions.md): every condition method, groups (`or`, `and`, `not`) and `c.restrictions`.
* [Associations](/v5.x/criteria-queries/criteria-builder/associations.md): dotted paths, joins, aliases and fetching.
* [Projections & Aggregates](/v5.x/criteria-queries/criteria-builder/projections.md): select columns, group, count and sum.
* [Modifiers](/v5.x/criteria-queries/criteria-builder/modifiers.md) and [Results](/v5.x/criteria-queries/criteria-builder/results.md): ordering, paging, caching, locking, result shapes and terminal methods.
* [SQL Log & Debugging](/v5.x/criteria-queries/criteria-builder/sql-log.md) and [Interception Events](/v5.x/criteria-queries/criteria-builder/interception-events.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://coldbox-orm.ortusbooks.com/v5.x/criteria-queries/criteria-builder.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
