# @Formula: a computed, read-only property (tables person, invoice, invoice_line)
# Hibernate ORM 7.4.17.Final, H2 2.4.240, JDK 25

== 1. A column-less property: no DDL, just a SQL fragment in the SELECT
-- columns of table person (information_schema)
   sql> select column_name from information_schema.columns where table_name = 'PERSON' order by column_name
   => [FIRST_NAME, ID, LAST_NAME]
-- find(Person, 1).getFullName()
   sql> select p1_0.id,p1_0.first_name,p1_0.first_name || ' ' || p1_0.last_name,p1_0.last_name from person p1_0 where p1_0.id=?
   => Asha Kulkarni

== 2. A correlated sub-select: computed on every load of the entity
-- find(Invoice, 1): total and lineCount
   sql> select i1_0.id,i1_0.customer,(select count(*) from invoice_line l where l.invoice_id = i1_0.id),(select coalesce(sum(l.qty * l.unit_price), 0) from invoice_line l where l.invoice_id = i1_0.id) from invoice i1_0 where i1_0.id=?
   => total=130.00 lineCount=2
-- find(Invoice, 3): an invoice with no lines
   sql> select i1_0.id,i1_0.customer,(select count(*) from invoice_line l where l.invoice_id = i1_0.id),(select coalesce(sum(l.qty * l.unit_price), 0) from invoice_line l where l.invoice_id = i1_0.id) from invoice i1_0 where i1_0.id=?
   => 0

== 3. Use it in HQL: the fragment is pasted into WHERE and ORDER BY as well
-- from Invoice i where i.total > 40 order by i.total desc
   sql> select i1_0.id,i1_0.customer,(select count(*) from invoice_line l where l.invoice_id = i1_0.id),(select coalesce(sum(l.qty * l.unit_price), 0) from invoice_line l where l.invoice_id = i1_0.id) from invoice i1_0 where (select coalesce(sum(l.qty * l.unit_price), 0) from invoice_line l where l.invoice_id = i1_0.id)>40 order by (select coalesce(sum(l.qty * l.unit_price), 0) from invoice_line l where l.invoice_id = i1_0.id) desc
   => [Acme, Globex]
   (the total sub-select appears 3 times in that one statement)

== 4. The price of loading it: a projection that skips the property skips the sub-select
-- select i.id, i.customer from Invoice i
   sql> select i1_0.id,i1_0.customer from invoice i1_0
   => 3
-- select i.id, i.customer, i.total from Invoice i
   sql> select i1_0.id,i1_0.customer,(select coalesce(sum(l.qty * l.unit_price), 0) from invoice_line l where l.invoice_id = i1_0.id) from invoice i1_0
   => 3

== 5. Staleness: the value is read once, with the entity
-- load Globex, add a 100.00 line, flush, read total, refresh, read total
   sql> select i1_0.id,i1_0.customer,(select count(*) from invoice_line l where l.invoice_id = i1_0.id),(select coalesce(sum(l.qty * l.unit_price), 0) from invoice_line l where l.invoice_id = i1_0.id) from invoice i1_0 where i1_0.id=?
   sql> insert into invoice_line (invoice_id,qty,unit_price,id) values (?,?,?,default)
   sql> select i1_0.id,i1_0.customer,(select count(*) from invoice_line l where l.invoice_id = i1_0.id),(select coalesce(sum(l.qty * l.unit_price), 0) from invoice_line l where l.invoice_id = i1_0.id) from invoice i1_0 where i1_0.id=?
   => before=50.00 afterFlush=50.00 afterRefresh=150.00

== 6. Read-only: assigning to the property sends nothing to the database
-- setFullName("HACK"); setLastName("Patil-Rao"); flush
   sql> select p1_0.id,p1_0.first_name,p1_0.first_name || ' ' || p1_0.last_name,p1_0.last_name from person p1_0 where p1_0.id=?
   sql> update person set first_name=?,last_name=? where id=?
   => full name in memory is now HACK
-- fresh session: find(Person, 2).getFullName()  (recomputed from the new last_name; HACK is gone)
   sql> select p1_0.id,p1_0.first_name,p1_0.first_name || ' ' || p1_0.last_name,p1_0.last_name from person p1_0 where p1_0.id=?
   => Ravi Patil-Rao
