---
title: "SQL-002: AuditNote cascade insert (CbpNote via Bill.Notes)"
summary: Attaches a category-B note to its parent bill; the CbpNote row is inserted by cascade at flush.
status: draft
owner: SAIF Doc Gen
last_reviewed: 2026-09-24
library: SAIF.Domain
reference_type: package
---

# SQL-002: AuditNote cascade insert (CbpNote via Bill.Notes)

When a category-B (bill-level) note is created, the `AuditNote` constructor attaches it to the parent `Bill`'s `Notes` collection instead of calling `Add` directly. The `CBP_NOTE` insert is carried out by the `CbpBill.Notes` cascade (`Cascade.AllDeleteOrphan`) when the caller's `IUnitOfWork` flushes*.

## Classification

| Field | Value |
| --- | --- |
| **SQL ID** | SQL-002 |
| **Operation type** | Create* |
| **Execution method** | ORM (cascade on flush*) |
| **Database** | Oracle* |
| **Schema** | [unqualified]** |
| **Command type** | N/A — ORM (NHibernate generates SQL at runtime from the ClassMaps in `Data/SAIF.Data.NHibernate/Mappings/**`) |
| **Business object** | `AuditNote` |
| **Criteria property** | N/A — ORM |
| **Lifecycle method** | `AuditNote` |

## Tables

| Table | Access | Columns used |
| --- | --- | --- |
| [CBP_NOTE](../database-tables.md#cbp_note) | INSERT | `CBP_NOTE_SEQ_NUM`, `UPDATE_NUM`, `ACTIVE_FLAG`, `CREATE_DATE`, `CREATE_USER`, `UPDATE_DATE`, `UPDATE_USER`, `NOTE_TXT`, `CLAIM_ID`, `NOTE_TYPE_CODE`, `NOTE_CATEGORY_CODE`, `BILL_ID` |

## LINQ statement

Source: `Domain/SAIF.Domain/Model/Claims/Bills/Notes/AuditNote.cs:60`

```csharp
// Domain/SAIF.Domain/Model/Claims/Bills/Notes/AuditNote.cs:55-62
if (noteArguments.CategoryCode == "B")
{
    var bill = _claimQueryProvider.BillQueries.GetBill(noteArguments.BillId);
    if (bill != null)
    {
        bill.AddNote(DataItem);
    }
}

// Data/SAIF.Data/Model/Claims/Bills/CbpBill.cs:134-138
public virtual void AddNote(CbpNote note)
{
    note.Bill = this;
    Notes.Add(note);
}
```

## 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 CBP_NOTE_SEQ.NEXTVAL
FROM DUAL

INSERT INTO CBP_NOTE (
  CBP_NOTE_SEQ_NUM,
  UPDATE_NUM,
  ACTIVE_FLAG,
  CREATE_DATE,
  CREATE_USER,
  UPDATE_DATE,
  UPDATE_USER,
  NOTE_TXT,
  CLAIM_ID,
  NOTE_TYPE_CODE,
  NOTE_CATEGORY_CODE,
  BILL_ID)
VALUES (
  :Id,
  :UpdateNumber,
  :IsActive,
  :CreateDate,
  :CreateUser,
  :UpdateDate,
  :UpdateUser,
  :NoteText,
  :ClaimId,
  :NoteTypeCode,
  :NoteTypeCategory,
  :Bill)
```

Translation notes:
- Stateful session (`OracleUnitOfWorkFactory` → `StatefulSessionStrategy`, confirmed via Legends): the child row is not inserted by an explicit repository call; it is inserted by NHibernate cascade when the owning entity is saved/flushed. Cascade source: `CbpBill.Notes` (`CbpBillMap.cs:92-95`, `Cascade.AllDeleteOrphan().Inverse()`).
- The collection is `Inverse()`, so `BILL_ID` is written from the child's `References(x => x.Bill)` (`CbpNoteMap.cs:30`), which `CbpBill.AddNote` sets (`CbpBill.cs:136`).
- `:Id` is the value fetched from `CBP_NOTE_SEQ` (`GeneratedBy.Native("CBP_NOTE_SEQ")` on Oracle).
- `:UpdateNumber` is the initial version value NHibernate seeds on insert (version column mapped with `Version(...)`).
- `:IsActive` is bound as `'Y'`/`'N'` (`YesNoType`).
- Path applies only when `noteArguments.CategoryCode == "B"`; `:ClaimId` is NULL on this path.
- `GetBill(noteArguments.BillId)` (line 57) is a separate read in `saif-data`, not part of this SQL ID.

## Operations and processes
- OP-DOM-15 AuditNote — attaches the note by category and inserts `CbpNote` (see [Operations and processes](../operations-processes.md))

## Business rules
- [DOM-BR-150](../business-rules.md#dom-br-150-note-create)

> **Note:** Statements marked with `*` were inferred from code structure, naming
> conventions, or contextual evidence where direct confirmation was not available
> in source artifacts. Statements marked with `**` could not be confirmed; treat
> as unknown until verified.

## Sources
- Domain/SAIF.Domain/Model/Claims/Bills/Notes/AuditNote.cs
