# Null handling for record embeddables
# Hibernate ORM 7.4.17.Final, H2 2.4.240, JDK 25

== A null embedded attribute is written as all-null columns
-- persist Product(1, Mouse, price = null)
   sql> insert into Product (name,amount,currency,id) values (?,?,?,?)
   => persisted
   in database: [1 | Mouse | null | null]
-- load it back
   sql> select p1_0.id,p1_0.name,p1_0.amount,p1_0.currency from Product p1_0 where p1_0.id=?
   => null

== Partly null columns give a record with null components
-- persist Money(null, INR), load it back
   sql> select p1_0.id,p1_0.name,p1_0.amount,p1_0.currency from Product p1_0 where p1_0.id=?
   => Money[amount=null, currency=INR]

== Querying for 'no price'
-- from Product p where p.price is null
   sql> select p1_0.id,p1_0.name,p1_0.amount,p1_0.currency from Product p1_0 where p1_0.amount is null and p1_0.currency is null
   => [1]
-- from Product p where p.price.amount is null
   sql> select p1_0.id,p1_0.name,p1_0.amount,p1_0.currency from Product p1_0 where p1_0.amount is null
   => [1, 2]

== Nested embeddable: Address with all-null Geo inside a non-null Address
-- load Customer 1 (geo columns all null, shipping columns all null)
   sql> select c1_0.id,c1_0.street,c1_0.city,c1_0.zip,c1_0.lat,c1_0.lon,c1_0.ship_street,c1_0.ship_city,c1_0.ship_zip,c1_0.ship_lat,c1_0.ship_lon from Customer c1_0 where c1_0.id=?
   => billing=Address[street=1 MG Road, city=Pune, zip=411001, geo=null]  shipping=null

== A compact constructor that rejects nulls (Weight)
-- persist Parcel(1, null) and load it
   sql> insert into Parcel (magnitude,unit,id) values (?,?,?)
   sql> select p1_0.id,p1_0.magnitude,p1_0.unit from Parcel p1_0 where p1_0.id=?
   => null
-- row with value null, unit 'kg': find Parcel 2
   sql> select p1_0.id,p1_0.magnitude,p1_0.unit from Parcel p1_0 where p1_0.id=?
   => InstantiationException: Could not instantiate entity 'com.ankurm.embeddables.Weight' due to: null  [root cause: NullPointerException: magnitude]

== hibernate.create_empty_composites.enabled=true (a setting often cited online for this; see output/10-setting-search.txt)
-- Product with all-null price columns, load it (Money has no validation)
   sql> select p1_0.id,p1_0.name,p1_0.amount,p1_0.currency from Product p1_0 where p1_0.id=?
   => null
   (same as the default above: the setting changed nothing)
-- Parcel with all-null weight columns, load it (Weight rejects nulls)
   sql> select p1_0.id,p1_0.magnitude,p1_0.unit from Parcel p1_0 where p1_0.id=?
   => null
