Add embeddables module: @Embeddable records in Hibernate 7.4.17.Final (columns and round trip, null handling, collections of embeddables and their SQL, record equality vs database equality, @EmbeddedId records, DDD Email value object, failure modes; H2)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b3obqqePfZ9m2Jk5Ub
This commit is contained in:
Claude
2026-11-26 18:14:43 +05:30
parent ec6eda8650
commit 4c795e8b50
45 changed files with 1453 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
# @Embeddable record Money(amount, currency) inside Product
# Hibernate ORM 7.4.17.Final, H2 2.4.240, JDK 25
== Columns of table product
AMOUNT | NUMERIC | null | 12 | 2 | YES
CURRENCY | CHARACTER VARYING | 3 | null | null | YES
ID | BIGINT | null | 64 | 0 | NO
NAME | CHARACTER VARYING | 255 | null | null | YES
== Insert and load
-- persist Product(1, Keyboard, Money(10.5, INR))
sql> insert into Product (name,amount,currency,id) values (?,?,?,?)
=> persisted
in database: [10.50 | INR | 1 | Keyboard]
-- load Product 1 in a new session
sql> select p1_0.id,p1_0.name,p1_0.amount,p1_0.currency from Product p1_0 where p1_0.id=?
=> Money[amount=10.50, currency=INR]
in-memory Money(10.5, INR).equals(Money loaded back from NUMERIC(12,2)) = false
== Updating: replace the whole record
-- replace price with Money(12.00, INR)
sql> select p1_0.id,p1_0.name,p1_0.amount,p1_0.currency from Product p1_0 where p1_0.id=?
sql> update Product set name=?,amount=?,currency=? where id=?
=> replaced
-- replace price with an equal-valued Money(12.0, INR) (scale 1 vs 2)
sql> select p1_0.id,p1_0.name,p1_0.amount,p1_0.currency from Product p1_0 where p1_0.id=?
=> replaced
record equals says: false
update issued: false
-- replace price with a really different Money(13.00, INR)
sql> select p1_0.id,p1_0.name,p1_0.amount,p1_0.currency from Product p1_0 where p1_0.id=?
sql> update Product set name=?,amount=?,currency=? where id=?
=> replaced
-- replace price with an identical new instance Money(13.00, INR)
sql> select p1_0.id,p1_0.name,p1_0.amount,p1_0.currency from Product p1_0 where p1_0.id=?
=> replaced
+20
View File
@@ -0,0 +1,20 @@
# The same record type embedded twice (Address as billing and shipping), with a nested record (Geo)
# Hibernate ORM 7.4.17.Final, H2 2.4.240, JDK 25
== Columns of table customer
LAT | DOUBLE PRECISION | null | 53 | null | YES
LON | DOUBLE PRECISION | null | 53 | null | YES
SHIP_LAT | DOUBLE PRECISION | null | 53 | null | YES
SHIP_LON | DOUBLE PRECISION | null | 53 | null | YES
ID | BIGINT | null | 64 | 0 | NO
CITY | CHARACTER VARYING | 255 | null | null | YES
SHIP_CITY | CHARACTER VARYING | 255 | null | null | YES
SHIP_STREET | CHARACTER VARYING | 255 | null | null | YES
SHIP_ZIP | CHARACTER VARYING | 255 | null | null | YES
STREET | CHARACTER VARYING | 255 | null | null | YES
ZIP | CHARACTER VARYING | 255 | null | null | YES
== Load it back
-- find Customer 1
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=Geo[lat=18.5, lon=73.8]] shipping=Address[street=9 Marine Dr, city=Mumbai, zip=400020, geo=null]
+47
View File
@@ -0,0 +1,47 @@
# 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
+14
View File
@@ -0,0 +1,14 @@
# A record with primitive components (Dim(int width, int height)) as an optional embedded attribute
# Hibernate ORM 7.4.17.Final, H2 2.4.240, JDK 25
== Columns of table box
HEIGHT | INTEGER | null | 32 | 0 | YES
WIDTH | INTEGER | null | 32 | 0 | YES
ID | BIGINT | null | 64 | 0 | NO
-- persist Box(1, Dim(3, 4))
sql> insert into Box (width,height,id) values (?,?,?)
=> persisted
-- persist Box(2, null)
sql> insert into Box (width,height,id) values (?,?,?)
=> persisted
rows: [4 | 3 | 1, null | null | 2]
+93
View File
@@ -0,0 +1,93 @@
# @ElementCollection of record Tag(name, colour): Set vs List (bag) vs List with @OrderColumn
# Hibernate ORM 7.4.17.Final, H2 2.4.240, JDK 25
== Columns of table article_tagset
ARTICLE_ID | BIGINT | null | 64 | 0 | NO
COLOUR | CHARACTER VARYING | 255 | null | null | YES
NAME | CHARACTER VARYING | 255 | null | null | YES
== Columns of table article_tagbag
ARTICLE_ID | BIGINT | null | 64 | 0 | NO
COLOUR | CHARACTER VARYING | 255 | null | null | YES
NAME | CHARACTER VARYING | 255 | null | null | YES
== Columns of table article_tagordered
TAGORDERED_ORDER | INTEGER | null | 32 | 0 | NO
ARTICLE_ID | BIGINT | null | 64 | 0 | NO
COLOUR | CHARACTER VARYING | 255 | null | null | YES
NAME | CHARACTER VARYING | 255 | null | null | YES
== Columns of table article_strictset
ARTICLE_ID | BIGINT | null | 64 | 0 | NO
COLOUR | CHARACTER VARYING | 255 | null | null | NO
NAME | CHARACTER VARYING | 255 | null | null | NO
primary key / unique constraints (table, columns):
ARTICLE_STRICTSET | PRIMARY KEY | ARTICLE_ID,COLOUR,NAME
ARTICLE_TAGORDERED | PRIMARY KEY | TAGORDERED_ORDER,ARTICLE_ID
== Removing one of three elements
-- Set: remove Tag(jpa, blue)
sql> select a1_0.id from Article a1_0 where a1_0.id=?
sql> select ts1_0.Article_id,ts1_0.name,ts1_0.colour from Article_tagSet ts1_0 where ts1_0.Article_id=?
sql> delete from Article_tagSet where Article_id=?
sql> insert into Article_tagSet (Article_id,name,colour) values (?,?,?)
sql> insert into Article_tagSet (Article_id,name,colour) values (?,?,?)
=> true
-- Set with NOT NULL components: remove NnTag(jpa, blue)
sql> select a1_0.id from Article a1_0 where a1_0.id=?
sql> select ss1_0.Article_id,ss1_0.name,ss1_0.colour from Article_strictSet ss1_0 where ss1_0.Article_id=?
sql> delete from Article_strictSet where Article_id=? and name=? and colour=?
=> true
-- List (bag): remove Tag(jpa, blue)
sql> select a1_0.id from Article a1_0 where a1_0.id=?
sql> select tb1_0.Article_id,tb1_0.name,tb1_0.colour from Article_tagBag tb1_0 where tb1_0.Article_id=?
sql> delete from Article_tagBag where Article_id=?
sql> insert into Article_tagBag (Article_id,name,colour) values (?,?,?)
sql> insert into Article_tagBag (Article_id,name,colour) values (?,?,?)
=> true
inserts after the delete: 2
-- List with @OrderColumn: remove Tag(jpa, blue)
sql> select a1_0.id from Article a1_0 where a1_0.id=?
sql> select to1_0.Article_id,to1_0.tagOrdered_ORDER,to1_0.name,to1_0.colour from Article_tagOrdered to1_0 where to1_0.Article_id=?
sql> delete from Article_tagOrdered where Article_id=? and tagOrdered_ORDER=?
sql> update Article_tagOrdered set name=?,colour=? where Article_id=? and tagOrdered_ORDER=?
=> true
== Adding one element
-- Set: add Tag(hql, grey)
sql> select a1_0.id from Article a1_0 where a1_0.id=?
sql> select ts1_0.Article_id,ts1_0.name,ts1_0.colour from Article_tagSet ts1_0 where ts1_0.Article_id=?
sql> delete from Article_tagSet where Article_id=?
sql> insert into Article_tagSet (Article_id,name,colour) values (?,?,?)
sql> insert into Article_tagSet (Article_id,name,colour) values (?,?,?)
sql> insert into Article_tagSet (Article_id,name,colour) values (?,?,?)
=> true
-- List (bag): add Tag(hql, grey)
sql> select a1_0.id from Article a1_0 where a1_0.id=?
sql> select tb1_0.Article_id,tb1_0.name,tb1_0.colour from Article_tagBag tb1_0 where tb1_0.Article_id=?
sql> delete from Article_tagBag where Article_id=?
sql> insert into Article_tagBag (Article_id,name,colour) values (?,?,?)
sql> insert into Article_tagBag (Article_id,name,colour) values (?,?,?)
sql> insert into Article_tagBag (Article_id,name,colour) values (?,?,?)
=> true
-- Set with NOT NULL components: add NnTag(hql, grey)
sql> select a1_0.id from Article a1_0 where a1_0.id=?
sql> select ss1_0.Article_id,ss1_0.name,ss1_0.colour from Article_strictSet ss1_0 where ss1_0.Article_id=?
sql> insert into Article_strictSet (Article_id,name,colour) values (?,?,?)
=> true
-- List with @OrderColumn: add Tag(hql, grey)
sql> select a1_0.id from Article a1_0 where a1_0.id=?
sql> select to1_0.Article_id,to1_0.tagOrdered_ORDER,to1_0.name,to1_0.colour from Article_tagOrdered to1_0 where to1_0.Article_id=?
sql> insert into Article_tagOrdered (Article_id,tagOrdered_ORDER,name,colour) values (?,?,?,?)
=> true
== Replacing the whole collection with an equal one
-- Set: replace with new LinkedHashSet of equal records
sql> select a1_0.id from Article a1_0 where a1_0.id=?
sql> select ts1_0.Article_id,ts1_0.name,ts1_0.colour from Article_tagSet ts1_0 where ts1_0.Article_id=?
sql> delete from Article_tagSet where Article_id=?
sql> insert into Article_tagSet (Article_id,name,colour) values (?,?,?)
sql> insert into Article_tagSet (Article_id,name,colour) values (?,?,?)
sql> insert into Article_tagSet (Article_id,name,colour) values (?,?,?)
=> 3
@@ -0,0 +1,31 @@
# Null components and null elements in a collection of records
# Hibernate ORM 7.4.17.Final, H2 2.4.240, JDK 25
-- Set<Tag>: add Tag(java, null)
sql> insert into Article (id) values (?)
sql> insert into Article_tagSet (Article_id,name,colour) values (?,?,?)
=> added
rows: [1 | null | java]
-- Set<Tag>: add Tag(null, null)
sql> select a1_0.id from Article a1_0 where a1_0.id=?
sql> select ts1_0.Article_id,ts1_0.name,ts1_0.colour from Article_tagSet ts1_0 where ts1_0.Article_id=?
sql> delete from Article_tagSet where Article_id=?
sql> insert into Article_tagSet (Article_id,name,colour) values (?,?,?)
sql> insert into Article_tagSet (Article_id,name,colour) values (?,?,?)
=> added
rows: [1 | null | null, 1 | null | java]
-- List<Tag> (bag): add Tag(null, null)
sql> select a1_0.id from Article a1_0 where a1_0.id=?
sql> select tb1_0.Article_id,tb1_0.name,tb1_0.colour from Article_tagBag tb1_0 where tb1_0.Article_id=?
sql> insert into Article_tagBag (Article_id,name,colour) values (?,?,?)
=> added
rows: [1 | null | null]
-- load tagBag
sql> select a1_0.id from Article a1_0 where a1_0.id=?
sql> select tb1_0.Article_id,tb1_0.name,tb1_0.colour from Article_tagBag tb1_0 where tb1_0.Article_id=?
=> []
(what a reader of the collection gets back)
-- List<Tag> (bag): add a null element
sql> select a1_0.id from Article a1_0 where a1_0.id=?
sql> select tb1_0.Article_id,tb1_0.name,tb1_0.colour from Article_tagBag tb1_0 where tb1_0.Article_id=?
=> added
rows: [1 | null | null]
+32
View File
@@ -0,0 +1,32 @@
# equals/hashCode of records, and comparing embeddables in HQL
# Hibernate ORM 7.4.17.Final, H2 2.4.240, JDK 25
Money(10.0, INR).equals(Money(10.00, INR)) = false
Money(10.00, INR).equals(Money(10.00, INR)) = true
hashCodes of 10.0 vs 10.00 Money equal = false
new HashSet(List.of(10.0, 10.00)).size() = 2
== Comparing an embeddable in HQL
-- from Product p where p.price = :m (m = Money(10.0, INR))
sql> select p1_0.id,p1_0.name,p1_0.amount,p1_0.currency from Product p1_0 where (p1_0.amount,p1_0.currency)=(?,?)
=> [1]
-- from Product p where p.price in (:a, :b)
sql> select p1_0.id,p1_0.name,p1_0.amount,p1_0.currency from Product p1_0 where (p1_0.amount,p1_0.currency) in ((?,?),(?,?))
=> [1, 2]
-- select p.price from Product p order by p.id (an embeddable as a result)
sql> select p1_0.amount,p1_0.currency from Product p1_0 order by p1_0.id
=> [Money[amount=10.00, currency=INR], Money[amount=20.00, currency=INR], null]
-- select p.price.amount from Product p order by p.price.amount desc
sql> select p1_0.amount from Product p1_0 order by p1_0.amount desc
=> [20.00, 10.00, null]
-- select new Money(p.price.amount, 'USD') from Product p where p.id = 1 (constructor expression)
sql> select p1_0.amount,'USD' from Product p1_0 where p1_0.id=1
=> [Money[amount=10.00, currency=USD]]
== A record as @EmbeddedId
Columns: [LINE | INTEGER | null | 32 | 0 | NO, ORDERID | BIGINT | null | 64 | 0 | NO, SKU | CHARACTER VARYING | 255 | null | null | YES]
-- find(OrderLine.class, new OrderLineId(7, 1))
sql> select ol1_0.orderId,ol1_0.line,ol1_0.sku from OrderLine ol1_0 where (ol1_0.orderId,ol1_0.line) in ((?,?))
=> SKU-A
-- same session twice: find with two equal-but-distinct keys, same instance?
sql> select ol1_0.orderId,ol1_0.line,ol1_0.sku from OrderLine ol1_0 where (ol1_0.orderId,ol1_0.line) in ((?,?))
=> true
+29
View File
@@ -0,0 +1,29 @@
# record Email(String value) as a DDD value object
# Hibernate ORM 7.4.17.Final, H2 2.4.240, JDK 25
== Columns of table member
ID | BIGINT | null | 64 | 0 | NO
EMAIL | CHARACTER VARYING | 255 | null | null | YES
stored value: [1 | [email protected]]
== Queries
-- from Member m where m.email = :e (e = new Email("[email protected]"))
sql> select m1_0.id,m1_0.email from Member m1_0 where m1_0.email=?
=> 1
-- from Member m where m.email.value like '%@example.com'
sql> select m1_0.id,m1_0.email from Member m1_0 where m1_0.email like '%@example.com' escape ''
=> 1
-- select m.email from Member m
sql> select m1_0.email from Member m1_0
=> [Email[[email protected]]]
== Bad data already in the table
-- find Member 2
sql> select m1_0.id,m1_0.email from Member m1_0 where m1_0.id=?
=> InstantiationException: Could not instantiate entity 'com.ankurm.embeddables.Email' due to: null [root cause: IllegalArgumentException: not an email address: not-an-address]
-- from Member (loads both rows)
sql> select m1_0.id,m1_0.email from Member m1_0 order by m1_0.id
=> InstantiationException: Could not instantiate entity 'com.ankurm.embeddables.Email' due to: null [root cause: IllegalArgumentException: not an email address: not-an-address]
-- select m.id from Member m (does not touch the Email)
sql> select m1_0.id from Member m1_0 order by m1_0.id
=> [1, 2]
+33
View File
@@ -0,0 +1,33 @@
# Things that go wrong with record embeddables
# Hibernate ORM 7.4.17.Final, H2 2.4.240, JDK 25
== Same record embedded twice without @AttributeOverride
org.hibernate.MappingException: Column 'street' is duplicated in mapping for entity 'com.ankurm.embeddables.BadCustomer' (use '@Column(insertable=false, updatable=false)' when mapping multiple properties to the same column)
== A component called value (record Handle(String value))
session factory started; columns of table account: []
-- persist Account(1, Handle(x))
sql> insert into Account (value,id) values (?,?)
=> SQLGrammarException: Could not prepare statement [Table "ACCOUNT" not found (this database is empty); SQL statement: [root cause: JdbcSQLSyntaxErrorException: Table "ACCOUNT" not found (this database is empty); SQL statement:]
== The same DDL typed by hand into H2 (create table t(value varchar(5)))
Syntax error in SQL statement "create table t([*]value varchar(5))"; expected "identifier"; SQL statement:
== A record that is not annotated @Embeddable, used with @Embedded (Holder.money is a PlainMoney)
session factory started; columns of table holder: [AMOUNT | NUMERIC | null | 38 | 2 | YES, ID | BIGINT | null | 64 | 0 | NO, CURRENCY | CHARACTER VARYING | 255 | null | null | YES]
-- persist Holder(1, PlainMoney(5, INR)) and load it
sql> insert into Holder (amount,currency,id) values (?,?,?)
sql> select h1_0.id,h1_0.amount,h1_0.currency from Holder h1_0 where h1_0.id=?
=> PlainMoney[amount=5.00, currency=INR]
== A record as @Entity
session factory started; columns of table recordentity: [ID | BIGINT | null | 64 | 0 | NO, NAME | CHARACTER VARYING | 255 | null | null | YES]
-- persist RecordEntity(1, first)
sql> insert into RecordEntity (name,id) values (?,?)
=> persisted
-- find it back
sql> select re1_0.id,re1_0.name from RecordEntity re1_0 where re1_0.id=?
=> InstantiationException: No default constructor for entity 'com.ankurm.embeddables.RecordEntity'
-- merge(new RecordEntity(1, second)) and find again
sql> select re1_0.id,re1_0.name from RecordEntity re1_0 where re1_0.id=?
=> InstantiationException: No default constructor for entity 'com.ankurm.embeddables.RecordEntity'
+5
View File
@@ -0,0 +1,5 @@
# hibernate-core-7.4.17.Final.jar: every class and resource searched for the text 'create_empty_composites'
$ grep -rl create_empty_composites <unzipped jar> | wc -l
0
$ grep -rl 'hibernate.use_nationalized_character_data' <unzipped jar> | wc -l # control: a real setting is found
3