Queries and HQL
Query your entities with HQL or simple filter-based lookups
bx-orm gives you two ways to query entities: HQL (Hibernate Query Language) for anything beyond a simple lookup, and filter-based queries for quick equality lookups without writing a query string at all.
HQL Queries
HQL looks like SQL, but it queries entity names and properties, not table and column names. Use ORMExecuteQuery to run one:
var toyotas = ORMExecuteQuery(
hql: "FROM Auto WHERE make = :make",
params: { make: "Toyota" }
);Parameters
Bind parameters using either named parameters (:name, with a struct of params) or positional parameters (?, with an array of params) - don't mix the two styles in the same query:
// Named
ORMExecuteQuery( hql: "FROM Auto WHERE make = :make", params: { make: "Toyota" } );
// Positional
ORMExecuteQuery( hql: "FROM Auto WHERE make = ?", params: [ "Toyota" ] );An array-valued parameter is expanded into a comma-separated list of placeholders automatically, so IN clauses work as expected:
ORMExecuteQuery(
hql: "FROM Auto WHERE make IN (:makes)",
params: { makes: [ "Toyota", "Honda", "Ford" ] }
);From 1.7.3, each value is converted to the type of the property it is compared with, so a string from a form or URL ("1") or a decimal binds to an integer property. Whole-number types only take whole numbers, and a value that cannot be converted ("abc" for an integer) still fails. See Typed Values from Forms and URLs.
Options
Pass a struct of options to control pagination, caching, and result shape:
unique
Return a single object (or null) instead of an array
offset
Skip this many results
maxResults
Limit the number of results returned
readOnly
Load results as read-only (not tracked for dirty-checking)
datasource
Run the query against a specific datasource, instead of the entity's default
Update and Delete Queries
HQL UPDATE and DELETE statements are executed directly against the database (bypassing the second-level cache and entity lifecycle events), and return the number of affected rows instead of a result list:
HQL in the Query Component and queryExecute()
<bx:query> (<cfquery> in CFML templates) and queryExecute() run HQL when you set dbtype="hql". They return the same result as ORMExecuteQuery: an array of entities, or an array of rows for a projection. They do not return a query object.
<bx:queryparam>(<cfqueryparam>) in the body binds a positional parameter. With asqltype(cfsqltype) the value is cast first, so an id that arrives as a string from a form matches an integer property.list="true"binds a list forIN ( ... ).paramsbinds named (struct) or positional (array) parameters, as inORMExecuteQuery().ormoptionstakes the query options, such asmaxResults,offset,readOnlyandunique.maxrows,timeoutanddatasourceapply too.resultgets a struct withsql(the HQL),recordCount,executionTimeandcached.
Without dbtype="hql", the query component and queryExecute() run SQL as usual.
Filter-Based Queries
For simple equality lookups, skip HQL entirely and use EntityLoad with a struct of filter criteria:
Every key in the filter struct must be a valid persistent property name on the entity; each key/value pair is combined with the others using AND equality. From 1.7.3, filter values are converted to each property's type, so { id: url.id } works when url.id is a string. This only supports equality checks - reach for HQL as soon as you need LIKE, ranges, joins, or OR logic.
EntityLoadByExample works the same way, but takes a sample entity instance instead of a struct:
Converting Results to a Query Object
If you need to hand entity results to code that expects a BoxLang Query object (e.g. for a report or grid), use EntityToQuery:
The query holds the entity's plain properties only, never relationship data. See Entities as Queries.
Where Queries Run
Both HQL and filter-based queries execute against the current request's Hibernate session - see Session Management for how that session is opened and when it flushes. If secondaryCacheEnabled is on for an entity, HQL queries respect the query cache automatically; see Caching.
Last updated
Was this helpful?