---
title: "SQL-259: Retrieve Policy"
summary: Read operation against Policy via NHibernate ORM.
status: draft
owner: SAIF Doc Gen
last_reviewed: 2026-09-24
library: SAIF.Services
reference_type: package
---

# SQL-259: Retrieve Policy

This statement reads a single `POLICY` row by policy number, used across several `CollectionsService` operations (market-type lookup, context validation, policy-debtor generation, and task creation) to load the policy behind a collections case or task. It is issued via NHibernate LINQ from `Services/SAIF.Services/Collections/CollectionsService.cs`, at four call sites that share the same shape.

## Classification

| Field | Value |
| --- | --- |
| **SQL ID** | SQL-259 |
| **Operation type** | Read |
| **Execution method** | ORM (NHibernate LINQ) |
| **Database** | Oracle |
| **Schema** | [unqualified]** |
| **Command type** | N/A (ORM-generated) |
| **Business object** | `CollectionsService` |
| **Criteria property** | N/A — ORM |
| **Lifecycle method** | `GetMarketType`, `ValidateContext`, `GeneratePolicyDebtor`, `CreateTask` |

## Parameters

| Parameter | Direction | Type | Max length | Description |
| --- | --- | --- | --- | --- |
| `:policyNum` | Input | long | N/A | Policy number (`policyNum` at L131, L909; `collectionCase.PolicyNum` at L1005) |
| `:PolicyNum` | Input | long | N/A | Policy number (`lcCase.PolicyNum` at L1378) |

## Tables

| Table | Access | Columns used |
| --- | --- | --- |
| [POLICY](../database-tables.md#policy) | SELECT | `POLICY_NUM`, `POLICY_NAME`, `LEGAL_NAME`, `SECURE_POLICY_CODE`, `OFFICE_CODE`, `ACCOUNT_NUM`, `POLICY_STATUS_CODE`, `MARKET_TYPE_CODE`, `POLICY_RELATIONSHIP_POLICY_NUM`, `POLICY_RELATIONSHIP_CODE`, `customer_service_team_code`, `ENTITY_CODE`, `TRADING_SEQ_NUM`, `RATE_ACCOUNT`, `AGENT_NUM` |

## LINQ statement

Source: `Services/SAIF.Services/Collections/CollectionsService.cs:131`

```csharp
// Services/SAIF.Services/Collections/CollectionsService.cs:131
var firstOrDefault = UnitOfWork.GetRepository<Policy>().Query().FirstOrDefault(x => x.PolicyNumber == policyNum);

// Services/SAIF.Services/Collections/CollectionsService.cs:909
contextName = UnitOfWork.GetRepository<Policy>().Query().First(x => x.PolicyNumber == policyNum).PolicyName;

// Services/SAIF.Services/Collections/CollectionsService.cs:1004-1005
var policy = UnitOfWork.GetRepository<Policy>().Query()
               .First(x => x.PolicyNumber == collectionCase.PolicyNum);

// Services/SAIF.Services/Collections/CollectionsService.cs:1378
taskData.Policy = UnitOfWork.GetRepository<Policy>().Query().SingleOrDefault(x => x.PolicyNumber == lcCase.PolicyNum);
```

## Sample SQL

> **Inferred SQL:** This SQL was inferred from the LINQ statement above and the NHibernate mappings. It is not concrete SQL from source code; NHibernate generates the executed statement at runtime, so aliases, column order, join syntax, and bind-parameter names may differ.

```sql
-- Inferred from LINQ (NHibernate mapping translation); not concrete SQL
-- GetMarketType (L131, FirstOrDefault), ValidateContext (L909, First), GeneratePolicyDebtor (L1004, First)
SELECT *
FROM (
  SELECT
    P.POLICY_NUM,
    P.POLICY_NAME,
    P.LEGAL_NAME,
    P.SECURE_POLICY_CODE,
    P.OFFICE_CODE,
    P.ACCOUNT_NUM,
    P.POLICY_STATUS_CODE,
    P.MARKET_TYPE_CODE,
    P.POLICY_RELATIONSHIP_POLICY_NUM,
    P.POLICY_RELATIONSHIP_CODE,
    P.customer_service_team_code,
    P.ENTITY_CODE,
    P.TRADING_SEQ_NUM,
    P.RATE_ACCOUNT,
    P.AGENT_NUM
  FROM POLICY P
  WHERE P.POLICY_NUM = :policyNum
)
WHERE ROWNUM <= 1

-- CreateTask (L1378, SingleOrDefault)
SELECT
  P.POLICY_NUM,
  P.POLICY_NAME,
  P.LEGAL_NAME,
  P.SECURE_POLICY_CODE,
  P.OFFICE_CODE,
  P.ACCOUNT_NUM,
  P.POLICY_STATUS_CODE,
  P.MARKET_TYPE_CODE,
  P.POLICY_RELATIONSHIP_POLICY_NUM,
  P.POLICY_RELATIONSHIP_CODE,
  P.customer_service_team_code,
  P.ENTITY_CODE,
  P.TRADING_SEQ_NUM,
  P.RATE_ACCOUNT,
  P.AGENT_NUM
FROM POLICY P
WHERE P.POLICY_NUM = :PolicyNum
-- NHibernate expects at most one row
```

Translation notes:
- `First`/`FirstOrDefault` → Oracle10 dialect row limit `SELECT * FROM (...) WHERE ROWNUM <= 1` (no `ORDER BY`, so the row returned is not deterministic if more than one matches).
- `Single`/`SingleOrDefault` → no row limit; NHibernate throws if more than one row is returned.
- `.PolicyName` (L909) is read from the materialized entity; the full entity is selected, not a projection.
- Expressions that do not reference the lambda parameter (captured locals, members of already-loaded objects, `Convert.ToInt64(...)`, `DateTime.Today`) are evaluated client-side by NHibernate's partial evaluator and bound as a single parameter value.
- `OFFICE_CODE` is mapped both as `Map(OfficeCode)` (`PolicyMap.cs:18`) and `References(Office)` (`PolicyMap.cs:26`); it is selected once. All other `References` contribute their FK column only; `HasMany` collections (`Periods`, `DbaNames`, `OscPolicies`, `SubmissionPolicies`, `TeamFunctions`, `BusinessAffiliates`) are lazy and not selected.

## Operations and processes
- CollectionsService.CreateTask — see [Operations and processes](../operations-processes.md)
- CollectionsService.GeneratePolicyDebtor — see [Operations and processes](../operations-processes.md)
- CollectionsService.GetMarketType — see [Operations and processes](../operations-processes.md)
- CollectionsService.ValidateContext — see [Operations and processes](../operations-processes.md)

## Sources
- `Services/SAIF.Services/Collections/CollectionsService.cs (L131, L909, L1004, L1378)`
