> 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/entityloadbypk.md).

# EntityLoadByPK

Load an entity, or several entities, by primary key

Load an entity by its primary key.

```js
var myAuto = entityLoadByPK( "Automobile", "1HGCM82633A123456" );
```

In Lucee, by default, an array of entities is returned and you must pass a third `unique=true` argument to return only a single entity. In BoxLang, only a single entity is returned - matching the Adobe ColdFusion behavior. A boolean third argument (Lucee's `unique`) is accepted and ignored. To return an array of entities, use the `entityLoad` BIF, or pass an array of ids.

Pass an array of ids to load several entities in one query. The result is an array in the order of the ids, with null where no row has that id:

```js
users = entityLoadByPK( "User", [ 3, 1, 99 ] ); // [ user3, user1, null ]
```

Composite keys are also supported:

```js
entityLoadByPK( "VehicleType", { make : "Ford", model: "Fusion" } );
```

## Options

The third argument is a struct of load options. They apply to every entity of an array of ids too:

| Option       | Description                                                                                                                                                                                              |
| ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `lock`       | Lock the row while loading it: `read`, `write` or `force` (see [EntityLock](/bxorm-2.x/reference/built-in-functions/entitylock.md)). Needs `transaction{}`, otherwise an `orm.argument` error is raised. |
| `timeout`    | Seconds to wait for the lock. `0` means do not wait. Only applies together with `lock`.                                                                                                                  |
| `skipLocked` | Return `null` instead of waiting when the row is locked by someone else. Only applies together with `lock`.                                                                                              |
| `readOnly`   | Load the entity read-only, so changes to it are never saved.                                                                                                                                             |

## Method Signature

```
EntityLoadByPK(entity=[String], id=[Any], options=[Any])
```

### Arguments

| Argument  | Type     | Required | Description                                                                                                                  | Default |
| --------- | -------- | -------- | ---------------------------------------------------------------------------------------------------------------------------- | ------- |
| `entity`  | `String` | `true`   | The name of the entity to load.                                                                                              |         |
| `id`      | `Any`    | `true`   | The primary key value, a struct of key/value pairs for composite keys, or an array of either.                                |         |
| `options` | `Any`    | `false`  | A struct of load options: `lock`, `timeout`, `skipLocked`, `readOnly`. A boolean (Lucee's `unique`) is accepted and ignored. |         |

## Examples

### Load by Primary Key

Load entity by primary key:

```java
vehicle = entityLoadByPK( "Vehicle", "1HGCM82633A123456" );
```

### Load by Composite Key

Pass a struct when the entity key is composite.

```java
vehicleType = entityLoadByPK( "VehicleType", { make: "Ford", model: "Fusion" } );
```

### Load Several by Primary Key

Pass an array of ids to load them all with one batched query. The array keeps the order you asked for, with `null` where no row has that id. An id asked for twice returns the same instance twice:

```java
users = entityLoadByPK( "User", [ 3, 1, 99 ] );
// [ user3, user1, null ]

types = entityLoadByPK( "VehicleType", [ { make: "Honda", model: "Civic" }, { make: "Ford", model: "Fusion" } ] );

reports = entityLoadByPK( "Report", ids, { readOnly : true } );
```

An empty array returns an empty array.

### Load and Lock

Lock the row until the transaction ends, so no one else can change it in the meantime:

```java
transaction {
    account = entityLoadByPK( "Account", id, { lock : "write" } );
    account.setBalance( account.getBalance() - amount );
}
```

Skip a row another transaction has locked instead of waiting for it:

```java
transaction {
    job = entityLoadByPK( "Job", id, { lock : "write", skipLocked : true } );
    if ( isNull( job ) ) {
        // someone else is working on it
    }
}
```

### Load Read-Only

```java
report = entityLoadByPK( "Report", id, { readOnly : true } );
```

To throw an `orm.notFound` error instead of returning `null`, use [EntityLoadByPKOrFail](/bxorm-2.x/reference/built-in-functions/entityloadbypkorfail.md).

## 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)
* [EntityLoad](/bxorm-2.x/reference/built-in-functions/entityload.md)
* [EntityLoadAsStruct](/bxorm-2.x/reference/built-in-functions/entityloadasstruct.md)
* [EntityLoadByExample](/bxorm-2.x/reference/built-in-functions/entityloadbyexample.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/entityloadbypk.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.
