---
title: "SQL-265: 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-265: Retrieve Policy

This statement covers four related `POLICY` security lookups in `PolicySecurityService`: a single-policy read by policy number (`IsValidForAgent`), an agent's producer book of policies by agent identifier (`ProducerBookList`), an agency book of policies for a list of active producer agents (`AgencyBookList`), and a policy-number-only projection for SAIF-secure policies (`SaifCorpSecurePolicies`). All four are issued via NHibernate LINQ from `Services/SAIF.Services/Policies/SecurityServices/PolicySecurityService.cs`.

## Classification

| Field | Value |
| --- | --- |
| **SQL ID** | SQL-265 |
| **Operation type** | Read |
| **Execution method** | ORM (NHibernate LINQ) |
| **Database** | Oracle |
| **Schema** | [unqualified]** |
| **Command type** | N/A (ORM-generated) |
| **Business object** | `PolicySecurityService` |
| **Criteria property** | N/A — ORM |
| **Lifecycle method** | `IsValidForAgent`, `ProducerBookList` (property), `AgencyBookList` (property), `SaifCorpSecurePolicies` |

## Parameters

| Parameter | Direction | Type | Max length | Description |
| --- | --- | --- | --- | --- |
| `:policyNumber` | Input | long | N/A | Policy number (`pol.PolicyNumber == policyNumber`) |
| `:CustomerId` | Input | long | N/A | Agent identifier (`Convert.ToInt64(CustomerId)`, joined via `Agent.Id`) |
| `:activeProducers` | Input | list of long | N/A | Active producer agent identifiers, expanded to one bind per element (`IN (...)`) |

## 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/Policies/SecurityServices/PolicySecurityService.cs:145`

```csharp
// Services/SAIF.Services/Policies/SecurityServices/PolicySecurityService.cs:145
var dataPolicy = unitOfWork.GetRepository<Policy>().Query().SingleOrDefault(pol => pol.PolicyNumber == policyNumber);

// Services/SAIF.Services/Policies/SecurityServices/PolicySecurityService.cs:204-206
var dataPolicy = unitOfWork.GetRepository<Policy>().Query()
    .Where(pol => pol.Agent.Id == Convert.ToInt64(CustomerId)).ToList();
producerBookList = dataPolicy.Distinct().Select(dp => dp.PolicyNumber).ToList();

// Services/SAIF.Services/Policies/SecurityServices/PolicySecurityService.cs:224-227
var dataPolicy = unitOfWork.GetRepository<Policy>().Query()
    .Where(pol => pol.Agent != null && activeProducers.Contains((long) pol.Agent.Id)).ToList();

agencyBookList = dataPolicy.Distinct().Select(dp => dp.PolicyNumber).ToList();

// Services/SAIF.Services/Policies/SecurityServices/PolicySecurityService.cs:235-237
var saifPolicyList = unitOfWork.GetRepository<Policy>().Query()
    .Where(pol => pol.SecurePolicyCode == SAIF_SECURE_POLICY_CODE)
    .Select(pol => pol.PolicyNumber).ToList();
```

## 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
-- IsValidForAgent (L145)
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 = :policyNumber
-- NHibernate expects at most one row

-- ProducerBookList (L204)
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.AGENT_NUM = :CustomerId

-- AgencyBookList (L224)
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.AGENT_NUM IS NOT NULL
  AND P.AGENT_NUM IN (:activeProducers)

-- SaifCorpSecurePolicies (L235)
SELECT
  P.POLICY_NUM
FROM POLICY P
WHERE P.SECURE_POLICY_CODE = 'SAIF'
```

Translation notes:
- 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.
- `activeProducers.Contains((long) pol.Agent.Id)` → `IN (...)` with one bind parameter per list element; the cast is a no-op in SQL.
- `SAIF_SECURE_POLICY_CODE = "SAIF"` (`PolicySecurityService.cs:21`) inlined as a literal.
- `.ToList()` materializes; `.Distinct().Select(dp => dp.PolicyNumber)` (L206, L227) run in memory. L237 `Select(pol => pol.PolicyNumber)` precedes `ToList()` and is a SQL projection.
- `Single`/`SingleOrDefault` → no row limit; NHibernate throws if more than one row is returned.
- `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
- PolicySecurityService.AgencyBookList (property) — see [Operations and processes](../operations-processes.md)
- PolicySecurityService.IsValidForAgent — see [Operations and processes](../operations-processes.md)
- PolicySecurityService.ProducerBookList (property) — see [Operations and processes](../operations-processes.md)
- PolicySecurityService.SaifCorpSecurePolicies — see [Operations and processes](../operations-processes.md)

## Sources
- `Services/SAIF.Services/Policies/SecurityServices/PolicySecurityService.cs (L145, L204, L224, L235)`
