> For the complete documentation index, see [llms.txt](https://bxorm.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bxorm.ortusbooks.com/bxorm-2.x/reference/built-in-functions/entityload.md).

# EntityLoad

Load an entity or an array of entities by id or filter

Load an entity or array of entities from the database.

You can pass a struct of query options, including pagination, caching, and sort options.

## Options

* **`unique`** - Boolean. Specifies whether to retrieve a single, unique item. Default is `false`. If more than one entity matches the filter, an `orm.query.nonUnique` error is raised.
* **`uniqueFirst`** - Boolean. Return the first match even when several entities match (implies `unique`). Default is `false`.
* **`ignorecase`** - Boolean. Sorts text properties without regard to case. Number and date properties are sorted as-is. Use it with a sort string in `uniqueOrOrder`. Defaults to `false`.
* **`offset`** - Number. Specifies the pagination offset. Defaults to 0.
* **`maxresults`** - Number. Specifies the maximum number of objects to be retrieved.
* **`cacheable`** - Boolean. Whether the result has to be cached in the second-level query cache (needs `secondaryCacheEnabled`). Default is `false`.
* **`cachename`** - String. The query cache region to use. Implies `cacheable` unless `cacheable` is given.
* **`timeout`** - Number. Specifies the timeout value (in seconds) for the query. No timeout by default.
* **`readOnly`** - Boolean. Load the entities read-only: they are not dirty-checked and changes to them are never saved. Works for filter loads and loads by id. Default is `false`. See also `entityLoadReadOnly()`.
* **`asStream`** - Boolean. Return a Java `Stream` that reads the entities from the database as it is consumed, instead of an array. Consume it in the same request. Not allowed with `unique` (`orm.argument`). Default is `false`.

For a load by id, only `readOnly` and `asStream` apply. The other options are for filter loads.

## Method Signature

```
EntityLoad(entityName=[string], idOrFilter=[any], uniqueOrOrder=[any], options=[struct])
```

### Arguments

| Argument        | Type     | Required | Description                                                                                                                                  | Default |
| --------------- | -------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| `entityName`    | `string` | `true`   | The name of the entity to load.                                                                                                              |         |
| `idOrFilter`    | `any`    | `false`  | Either the ID of the entity to load, or a struct of filter criteria.                                                                         |         |
| `uniqueOrOrder` | `any`    | `false`  | A boolean (return one entity), or a comma-separated sort string such as `"lastName desc, firstName"`. A struct here is treated as `options`. |         |
| `options`       | `struct` | `false`  | A struct of options to modify the load operation. See below for supported options.                                                           |         |

## Examples

## Loading an Entity Array

To load an array of Autos from the database where the `Make` is `Ford`, you would do the following:

```java
var allFords = entityLoad( "Auto", { Make = "Ford" } );
```

By default, all matching records are returned. If you want to paginate the result set, you can pass a struct with query options as the fourth argument:

```java
var firstTenFords = entityLoad(
    "Auto",                         // first arg: entity name
    { Make = "Ford" },              // second arg: criteria struct
    false,                          // third arg: false for "not unique"
    { maxResults = 10, offset = 0 } // fourth arg: option struct
);
```

### Loading Unique Entities

We can also load single, unique entity results by passing `true` in the third argument to indicate that we want a single, unique result. For example, to load an entity named `Auto` with an ID of `123`, you would do the following:

```java
var myEntity = entityLoad( "Auto", 123, true );
```

This will retrieve the entity from the database and populate the `myEntity` variable with its data. If the entity is not found, `null` is returned.

You can also use the `entityLoad()` function to load an entity by its unique properties, even if they are not the primary key. For example, to load an `Auto` entity with a specific `VIN`, you could do the following:

```java
var myEntity = entityLoad( "Auto", { VIN = "1HGCM82633A123456" }, true );
```

This will search for the `Auto` entity with the specified `VIN` and return it if found.

### When Nothing Matches

`entityLoad()` does not throw when nothing is found. A unique load returns `null`, and any other load returns an empty array. Use [EntityLoadOrFail](/bxorm-2.x/reference/built-in-functions/entityloadorfail.md) to get an `orm.notFound` error instead.

### Sorting

Pass a comma-separated sort string as the third argument:

```java
var people = entityLoad( "Person", { active = true }, "lastName desc, firstName" );
```

### Loading Read-Only Entities

Pass `readOnly : true` to load entities that are not dirty-checked. Changes made to them are never saved, which keeps large reads light:

```java
var shipped = entityLoad( "Order", { status = "shipped" }, "createdDate desc", { readOnly = true } );
```

[EntityLoadReadOnly](/bxorm-2.x/reference/built-in-functions/entityloadreadonly.md) does the same without the option.

## Related

* [EntityCriteria](/bxorm-2.x/reference/built-in-functions/entitycriteria.md)
* [EntityDelete](/bxorm-2.x/reference/built-in-functions/entitydelete.md)
* [EntityEvict](/bxorm-2.x/reference/built-in-functions/entityevict.md)
* [EntityGetDatasource](/bxorm-2.x/reference/built-in-functions/entitygetdatasource.md)
* [EntityGetDirtyProperties](/bxorm-2.x/reference/built-in-functions/entitygetdirtyproperties.md)
* [EntityGetId](/bxorm-2.x/reference/built-in-functions/entitygetid.md)
* [EntityGetMetadata](/bxorm-2.x/reference/built-in-functions/entitygetmetadata.md)
* [EntityGetName](/bxorm-2.x/reference/built-in-functions/entitygetname.md)
* [EntityGetReference](/bxorm-2.x/reference/built-in-functions/entitygetreference.md)
* [EntityIsAttached](/bxorm-2.x/reference/built-in-functions/entityisattached.md)
* [EntityIsDirty](/bxorm-2.x/reference/built-in-functions/entityisdirty.md)
* [EntityLoadAsStruct](/bxorm-2.x/reference/built-in-functions/entityloadasstruct.md)
* [EntityLoadByExample](/bxorm-2.x/reference/built-in-functions/entityloadbyexample.md)
* [EntityLoadByPK](/bxorm-2.x/reference/built-in-functions/entityloadbypk.md)
* [EntityLoadByPKOrFail](/bxorm-2.x/reference/built-in-functions/entityloadbypkorfail.md)
* [EntityLoadOrFail](/bxorm-2.x/reference/built-in-functions/entityloadorfail.md)
* [EntityLoadOrNew](/bxorm-2.x/reference/built-in-functions/entityloadornew.md)
* [EntityLoadOrSave](/bxorm-2.x/reference/built-in-functions/entityloadorsave.md)
* [EntityLoadReadOnly](/bxorm-2.x/reference/built-in-functions/entityloadreadonly.md)
* [EntityLock](/bxorm-2.x/reference/built-in-functions/entitylock.md)
* [EntityMerge](/bxorm-2.x/reference/built-in-functions/entitymerge.md)
* [EntityNameArray](/bxorm-2.x/reference/built-in-functions/entitynamearray.md)
* [EntityNameList](/bxorm-2.x/reference/built-in-functions/entitynamelist.md)
* [EntityNew](/bxorm-2.x/reference/built-in-functions/entitynew.md)
* [EntityReload](/bxorm-2.x/reference/built-in-functions/entityreload.md)
* [EntitySave](/bxorm-2.x/reference/built-in-functions/entitysave.md)
* [EntityToQuery](/bxorm-2.x/reference/built-in-functions/entitytoquery.md)
* [EntityToStruct](/bxorm-2.x/reference/built-in-functions/entitytostruct.md)
* [ORMClearSession](/bxorm-2.x/reference/built-in-functions/ormclearsession.md)
* [ORMCloseAllSessions](/bxorm-2.x/reference/built-in-functions/ormcloseallsessions.md)
* [ORMCloseSession](/bxorm-2.x/reference/built-in-functions/ormclosesession.md)
* [ORMDiagnostics](/bxorm-2.x/reference/built-in-functions/ormdiagnostics.md)
* [ORMEvictCollection](/bxorm-2.x/reference/built-in-functions/ormevictcollection.md)
* [ORMEvictEntity](/bxorm-2.x/reference/built-in-functions/ormevictentity.md)
* [ORMEvictQueries](/bxorm-2.x/reference/built-in-functions/ormevictqueries.md)
* [ORMExecuteQuery](/bxorm-2.x/reference/built-in-functions/ormexecutequery.md)
* [ORMFlush](/bxorm-2.x/reference/built-in-functions/ormflush.md)
* [ORMFlushAll](/bxorm-2.x/reference/built-in-functions/ormflushall.md)
* [ORMGetHibernateVersion](/bxorm-2.x/reference/built-in-functions/ormgethibernateversion.md)
* [ORMGetSQLFunctions](/bxorm-2.x/reference/built-in-functions/ormgetsqlfunctions.md)
* [ORMGetSession](/bxorm-2.x/reference/built-in-functions/ormgetsession.md)
* [ORMGetSessionFactory](/bxorm-2.x/reference/built-in-functions/ormgetsessionfactory.md)
* [ORMGetSessionStatistics](/bxorm-2.x/reference/built-in-functions/ormgetsessionstatistics.md)
* [ORMIsSessionDirty](/bxorm-2.x/reference/built-in-functions/ormissessiondirty.md)
* [ORMReadOnly](/bxorm-2.x/reference/built-in-functions/ormreadonly.md)
* [ORMReload](/bxorm-2.x/reference/built-in-functions/ormreload.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://bxorm.ortusbooks.com/bxorm-2.x/reference/built-in-functions/entityload.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.
