---
title: "SQL-227: Retrieve LegalCase via SaveSubpoena"
summary: Load the LegalCase referenced by a subpoena's case ID before saving the subpoena.
status: draft
owner: SAIF Doc Gen
last_reviewed: 2026-09-24
library: SAIF.Services
reference_type: package
---

# SQL-227: Retrieve LegalCase via SaveSubpoena

`SubpoenaSaveService.SaveSubpoena` loads `LegalCase` by primary key to attach it to `dataSubpoena.LegalCase`. It runs only when the subpoena is new or its case ID changed (`isNew || subpoena.CaseId != dataSubpoena.LegalCase.Id`).

## Classification

| Field | Value |
| --- | --- |
| **SQL ID** | SQL-227 |
| **Operation type** | Read |
| **Execution method** | ORM (NHibernate LINQ) |
| **Database** | Oracle |
| **Schema** | [unqualified]** |
| **Command type** | N/A (ORM-generated) |
| **Business object** | `LegalCase` |
| **Criteria property** | N/A — ORM |
| **Lifecycle method** | `SaveSubpoena` |

## Parameters

| Parameter | Direction | Type | Max length | Description |
| --- | --- | --- | --- | --- |
| `:caseId` | Input | long | N/A | `subpoena.CaseId` |

## Tables

| Table | Access | Columns used |
| --- | --- | --- |
| [LEGAL_CASE](../database-tables.md#legal_case) | SELECT | all 17 mapped columns; filter LEGAL_CASE_SEQ_NUM |

`dataSubpoena.LegalCase.WorkerAttorneyFirm.Addresses.Any()` (L127-129) is evaluated in memory and triggers separate lazy loads of the attorney-firm provider location and its addresses; these are not joined here.

## LINQ statement

Source: `Services/SAIF.Services/Legal/Subpoenas/SubpoenaSaveService.cs:124`

```csharp
dataSubpoena.LegalCase = _unitOfWork.GetRepository<LegalData.LegalCase>()
    .Query().SingleOrDefault(x => x.Id == subpoena.CaseId);
```

## 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
SELECT LC.LEGAL_CASE_SEQ_NUM,
       LC.CLAIM_ID,
       LC.WCB_NUM,
       LC.DCBS_NUM,
       LC.OALJ_NUM,
       LC.OWCP_NUM,
       LC.BRB_NUM,
       LC.REQUEST_DATE,
       LC.CLAIM_IMAGE_ALERTS_FLAG,
       LC.CREATE_USER,
       LC.CREATE_DATE,
       LC.UPDATE_USER,
       LC.UPDATE_DATE,
       LC.MOST_RECENT_HEARING_SEQ_NUM,
       LC.CASE_STATUS_CODE,
       LC.WORKER_ATTY_PROV_LOC_ID,
       LC.WORKER_ATTY_FIRM_PROV_LOC_ID
FROM   LEGAL_CASE LC
WHERE  LC.LEGAL_CASE_SEQ_NUM = :caseId
-- NHibernate expects at most one row
```

Translation notes:
- `SingleOrDefault`/`Single` add no row limit; NHibernate throws if more than one row is returned.
- Runs only when `isNew || subpoena.CaseId != dataSubpoena.LegalCase.Id` (L122).

## Operations and processes
- SubpoenaSaveService.SaveSubpoena — see [Operations and processes](../operations-processes.md)

## Business rules
- [SVC-BR-LGL-095](../business-rules.md#svc-br-lgl-095-subpoena%3A-subpoenatype.code-%22error%3A-subpoena-type-must-be-designated.%22%3B-recipients.count%3E0-%22error%3A-a-subpoena-must-have-at-least-one-recipient.%22%3B-appearancedate-%22error%3A-a-subpoena-must-have-an-appearance-date.%22%3B-appearancetime-%22error%3A-a-subpoena-must-have-an-appearance-time.%22%3B-hearinglocation-%22error%3A-a-subpoena-must-have-a-hearing-location-selected.%22%3B-compliancedate-%22error%3A-a-subpoena-must-have-a-compliance-date.%22%3B-specialhandling-%22error%3A-a-subpoena-must-have-a-special-handling-type-selected.%22%3B-requestedbyemployeeid-%22error%3A-a-subpoena-must-have-a-requesting-attorney.%22%3B-recordsfromdate-required-when-thru-present%3B-recordsthrudate-required-when-from-present%3B-thru-%E2%89%A5-from-%22error%3A-records-thru-date-cannot-be-before-records-from-date.%22%3B-recipient-data-%22error%3A-invalid-or-incomplete-witness-information-entered.%22)
- [SVC-BR-LGL-096](<../business-rules.md#svc-br-lgl-096-recipient-data%3A-records%2Fclaims-types-need-providerlocationid%3E0-and-a-9-char-tin%3B-othermedical%2Fparties%2Fnonparties-need-first-name-1%E2%80%9320%2C-last-name-1%E2%80%9325%2C-and-a-policy-location-or-a-full-address-(line1-5%E2%80%9340%2C-line2-%E2%89%A440%2C-city-2%E2%80%9330%2C-state-2%2C-zip-5%2C-%2B4-empty-or-4)%3B-other-types-invalid>)
- [SVC-BR-LGL-097](../business-rules.md#svc-br-lgl-097-recipient%3A-mileagecost-%E2%89%A50-%22error%3A-mileage-cost-cannot-be-negative.%22%3B-mileageunits-%E2%89%A50-%22error%3A-mileage-units-cannot-be-negative.%22%3B-paidamount-0-or-%E2%89%A5-calculatedpaidamount-%22error%3A-the-paid-amount-cannot-be-less-than-the-calculated-amount.%22)

## Sources
- `Services/SAIF.Services/Legal/Subpoenas/SubpoenaSaveService.cs (L124)`
