Join our Newsletter — 33% off our NHI Course
Home› Glossary› Architecture & Implementation› Criteria Query
Architecture & Implementation

Criteria Query

← Back to Glossary
By NHI Mgmt Group Updated November 25, 2026 Domain: Architecture & Implementation

Criteria Query is a programmatic query built from structured criteria rather than handwritten text. It is used in application frameworks and object-relational mapping systems to assemble database filters, sorting and joins through an API, which reduces direct string concatenation and makes queries easier to compose safely.

What a Criteria Query is used for

A criteria query gives developers a structured way to define database conditions, joins, and sort order through an API instead of writing ad hoc query strings. That makes the query intent easier to assemble, reuse, and validate inside the application layer, especially in frameworks that generate SQL from object-oriented criteria.

The main value of this pattern is composability. Rather than concatenating text fragments, developers combine predicates, parameters, and ordering rules as objects, which helps keep query construction consistent across code paths and reduces the chance that one branch of the application builds a slightly different filter than another.

How criteria queries differ from handwritten SQL

Criteria queries sit between business logic and the database driver. The application expresses what data it wants through structured conditions, while the framework translates that structure into SQL or a provider-specific query language.

That abstraction can improve maintainability, but it also means the developer must understand the framework’s translation rules. A criteria API can express many common query shapes, yet highly specialised SQL features may still require native queries or provider extensions.

Why criteria queries matter for query safety and correctness

Because criteria queries are built from structured inputs, they help avoid direct string assembly for filters and sort clauses. That lowers the risk of injection mistakes and makes parameter handling more predictable, particularly in large codebases where many modules build database queries.

They also support correctness by making query structure explicit. When filters, joins, and ordering are assembled as typed or well-defined objects, it is easier to review what the application will send to the database and to spot accidental ambiguity, missing predicates, or inconsistent join logic.

Used well, this approach can improve both security and developer ergonomics, but it is not a substitute for authorization checks, input validation, or safe handling of sensitive data returned by the query.

Where criteria queries fit in application design

Criteria queries are most useful when an application needs dynamic filtering, optional search fields, or reusable query fragments. They are common in ORM-heavy systems where the codebase benefits from a single query-building style rather than many hand-written SQL variants.

They are less helpful when the query is simple and static, or when the database-specific optimisation strategy matters more than abstraction. In those cases, a direct query may be clearer and easier to tune.

Risk and Threat Considerations

Criteria queries reduce some of the risk that comes with handwritten SQL, but they do not remove database exposure. If developers misuse raw fragments, unsafe sort expressions, or provider-specific escape hatches, the application can still inherit injection, access-control, or data-leakage problems.

Failure mechanism: Attackers or flawed application logic can exploit any place where the criteria API falls back to untrusted text, weakly validated field names, or overly broad joins, turning a supposedly structured query path into an unsafe one.

Impact: The result can be unauthorized data access, incorrect filtering, unexpected record disclosure, or application behaviour that is harder to detect because the query is generated indirectly rather than written in one obvious statement.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP ASVS and NIST SP 800-53 Rev 5 set the governance and control requirements practitioners need to meet.

FrameworkControl / ReferenceRelevance
OWASP ASVSV1 — Encoding and SanitizationStructured query building helps avoid unsafe text assembly in data-access paths.
V8 — AuthorizationCriteria queries often enforce record-level access rules that must remain consistent with authorization decisions.
Recommendation — Validate and parameterize all data-driven query inputs before they reach the query builder. Verify that every criteria-based data access path enforces the same authorization rules as the rest of the application.
NIST SP 800-53 Rev 5SI-10 — Information Input ValidationCriteria queries depend on validating externally influenced values used in filters and sort keys.
AC-6 — Least PrivilegeGenerated queries can expose more data than intended if database access is broader than necessary.
Recommendation — Validate query inputs before they are mapped into criteria predicates or ordering logic. Limit application database permissions to the minimum tables, columns, and operations required.

Practitioner Guidance

Why practitioners should care: Criteria queries are best treated as a safer construction pattern, not an automatic security control. The important judgement is whether the framework usage keeps all user influence inside typed predicates and parameter binding, or whether it quietly reintroduces string-based query assembly.

What to watch for: Pay close attention to dynamic ordering, optional joins, and any API call that accepts raw expressions, because those are the points where query structure can become unsafe or difficult to reason about.

Practitioner takeaway: Use criteria queries to standardise query construction, but still review the generated SQL paths as carefully as any other data-access code.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

NHIMG Editorial Note
Reviewed and updated by the NHIMG editorial team on November 25, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org