Add one-to-many module: @OneToMany in Hibernate 7.4.16.Final (unidirectional join table vs @JoinColumn vs mappedBy, Set vs List removal cost, sync helper, fetch defaults, N+1, MultipleBagFetchException fixes, id-hashCode trap)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b4H5ph3J7k2MJnRuVH
This commit is contained in:
asmhatre
2026-11-16 10:49:49 +05:30
co-authored by Claude Sonnet 5.5
parent bdae169002
commit eab5461d86
24 changed files with 1560 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
# One parent, two children, four mappings: tables and insert SQL
# Hibernate ORM 7.4.16.Final, H2, JDK 25
== A. Unidirectional, plain @OneToMany (no mappedBy, no @JoinColumn)
tables: [JTCHILD, JTLISTPARENT, JTLISTPARENT_JTCHILD]
-- persist(parent with 2 children); commit
sql> insert into JtListParent (id) values (default)
sql> insert into JtChild (name,id) values (?,default)
sql> insert into JtChild (name,id) values (?,default)
sql> insert into JtListParent_JtChild (JtListParent_id,children_id) values (?,?)
sql> insert into JtListParent_JtChild (JtListParent_id,children_id) values (?,?)
== B. Unidirectional with @JoinColumn(name = "parent_id")
tables: [JCCHILD, JCLISTPARENT]
-- persist(parent with 2 children); commit
sql> insert into JcListParent (id) values (default)
sql> insert into JcChild (name,id) values (?,default)
sql> insert into JcChild (name,id) values (?,default)
sql> update JcChild set parent_id=? where id=?
sql> update JcChild set parent_id=? where id=?
== C. Bidirectional, mappedBy, child owns the foreign key
tables: [DEPARTMENTS, EMPLOYEES]
-- persist(department with 2 employees); commit
sql> insert into departments (name,id) values (?,default)
sql> insert into employees (dept_id,name,uuid,id) values (?,?,?,default)
sql> insert into employees (dept_id,name,uuid,id) values (?,?,?,default)
+59
View File
@@ -0,0 +1,59 @@
# Removing ONE child: statements sent, per mapping
# Hibernate ORM 7.4.16.Final, H2, JDK 25
Each row: mapping | children before | statements on remove(one) + flush() | of which DELETE | of which INSERT | of which UPDATE
== 5 children
join table, List (bag) | 5 | writes= 6 | delete=2 insert=4 update=0
-- join table, List, n=5
sql> select jlp1_0.id from JtListParent jlp1_0 where jlp1_0.id=?
sql> select c1_0.JtListParent_id,c1_1.id,c1_1.name from JtListParent_JtChild c1_0 join JtChild c1_1 on c1_1.id=c1_0.children_id where c1_0.JtListParent_id=?
sql> delete from JtListParent_JtChild where JtListParent_id=?
sql> insert into JtListParent_JtChild (JtListParent_id,children_id) values (?,?)
sql> insert into JtListParent_JtChild (JtListParent_id,children_id) values (?,?)
sql> insert into JtListParent_JtChild (JtListParent_id,children_id) values (?,?)
sql> insert into JtListParent_JtChild (JtListParent_id,children_id) values (?,?)
sql> delete from JtChild where id=?
join table, Set | 5 | writes= 2 | delete=2 insert=0 update=0
-- join table, Set, n=5
sql> select jsp1_0.id from JtSetParent jsp1_0 where jsp1_0.id=?
sql> select c1_0.JtSetParent_id,c1_1.id,c1_1.name from JtSetParent_JtChild c1_0 join JtChild c1_1 on c1_1.id=c1_0.children_id where c1_0.JtSetParent_id=?
sql> delete from JtSetParent_JtChild where JtSetParent_id=? and children_id=?
sql> delete from JtChild where id=?
@JoinColumn, List (bag) | 5 | writes= 2 | delete=1 insert=0 update=1
-- @JoinColumn, List, n=5
sql> select jlp1_0.id from JcListParent jlp1_0 where jlp1_0.id=?
sql> select c1_0.parent_id,c1_0.id,c1_0.name from JcChild c1_0 where c1_0.parent_id=?
sql> update JcChild set parent_id=null where parent_id=? and id=?
sql> delete from JcChild where id=?
@JoinColumn + @OrderColumn, remove FIRST | 5 | writes= 10 | delete=1 insert=0 update=9
-- @OrderColumn, remove first, n=5
sql> select op1_0.id from OcParent op1_0 where op1_0.id=?
sql> select c1_0.parent_id,c1_0.pos,c1_0.id,c1_0.name from OcChild c1_0 where c1_0.parent_id=?
sql> update OcChild set parent_id=null,pos=null where parent_id=? and id=?
sql> update OcChild set parent_id=null,pos=null where parent_id=? and id=?
sql> update OcChild set parent_id=null,pos=null where parent_id=? and id=?
sql> update OcChild set parent_id=null,pos=null where parent_id=? and id=?
sql> update OcChild set parent_id=null,pos=null where parent_id=? and id=?
sql> update OcChild set parent_id=?,pos=? where id=?
sql> update OcChild set parent_id=?,pos=? where id=?
sql> update OcChild set parent_id=?,pos=? where id=?
sql> update OcChild set parent_id=?,pos=? where id=?
sql> delete from OcChild where id=?
bidirectional mappedBy, List (bag) | 5 | writes= 1 | delete=1 insert=0 update=0
-- bidirectional, List, n=5
sql> select bld1_0.id from BiListDept bld1_0 where bld1_0.id=?
sql> select e1_0.dept_id,e1_0.id,e1_0.name from BiListEmp e1_0 where e1_0.dept_id=?
sql> delete from BiListEmp where id=?
bidirectional mappedBy, Set | 5 | writes= 1 | delete=1 insert=0 update=0
-- bidirectional, Set, n=5
sql> select d1_0.id,d1_0.name from departments d1_0 where d1_0.id=?
sql> select e1_0.dept_id,e1_0.id,e1_0.name,e1_0.uuid from employees e1_0 where e1_0.dept_id=?
sql> delete from employees where id=?
== 50 children
join table, List (bag) | 50 | writes= 51 | delete=2 insert=49 update=0
join table, Set | 50 | writes= 2 | delete=2 insert=0 update=0
@JoinColumn, List (bag) | 50 | writes= 2 | delete=1 insert=0 update=1
@JoinColumn + @OrderColumn, remove FIRST | 50 | writes=100 | delete=1 insert=0 update=99
bidirectional mappedBy, List (bag) | 50 | writes= 1 | delete=1 insert=0 update=0
bidirectional mappedBy, Set | 50 | writes= 1 | delete=1 insert=0 update=0
+5
View File
@@ -0,0 +1,5 @@
# Bidirectional sync: parent side only vs helper method
# Hibernate ORM 7.4.16.Final, H2, JDK 25
in memory, before flush: e.department == null
database after commit: employee 1 has dept_id = null
database after commit: employee 2 (via helper) has dept_id = 2
@@ -0,0 +1,6 @@
# JPA fetch defaults: @OneToMany is LAZY, @ManyToOne is EAGER
# Hibernate ORM 7.4.16.Final, H2, JDK 25
-- find(EagerEmployee) with @ManyToOne at its default
sql> select ee1_0.id,d1_0.id,d1_0.name,ee1_0.name from EagerEmployee ee1_0 left join EagerDepartment d1_0 on d1_0.id=ee1_0.dept_id where ee1_0.id=?
-- find(EagerDepartment): the @OneToMany collection is not loaded
sql> select ed1_0.id,ed1_0.name from EagerDepartment ed1_0 where ed1_0.id=?
+15
View File
@@ -0,0 +1,15 @@
# N+1 and its fixes: statements sent for 5 departments with 3 employees each
# Hibernate ORM 7.4.16.Final, H2, JDK 25
-- naive: query departments, then touch each employees collection
sql> select d1_0.id,d1_0.name from departments d1_0
sql> select e1_0.dept_id,e1_0.id,e1_0.name,e1_0.uuid from employees e1_0 where e1_0.dept_id=?
sql> select e1_0.dept_id,e1_0.id,e1_0.name,e1_0.uuid from employees e1_0 where e1_0.dept_id=?
sql> select e1_0.dept_id,e1_0.id,e1_0.name,e1_0.uuid from employees e1_0 where e1_0.dept_id=?
sql> select e1_0.dept_id,e1_0.id,e1_0.name,e1_0.uuid from employees e1_0 where e1_0.dept_id=?
sql> select e1_0.dept_id,e1_0.id,e1_0.name,e1_0.uuid from employees e1_0 where e1_0.dept_id=?
departments returned: 5 (no DISTINCT needed in Hibernate 7)
-- fix 1: left join fetch d.employees
sql> select d1_0.id,e1_0.dept_id,e1_0.id,e1_0.name,e1_0.uuid,d1_0.name from departments d1_0 left join employees e1_0 on d1_0.id=e1_0.dept_id
-- fix 2: @BatchSize(size = 20) on the collection, same loop as the naive one
sql> select bd1_0.id from BatchDept bd1_0
sql> select e1_0.dept_id,e1_0.id,e1_0.name from BatchEmp e1_0 where e1_0.dept_id in (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
+27
View File
@@ -0,0 +1,27 @@
# MultipleBagFetchException and three fixes (3 employees, 4 projects per department)
# Hibernate ORM 7.4.16.Final, H2, JDK 25
== The failure: two Lists, two join fetches
exception: java.lang.IllegalArgumentException <- org.hibernate.loader.MultipleBagFetchException
message: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags: [com.ankurm.onetomany.Variants$BagDept.employees, com.ankurm.onetomany.Variants$BagDept.projects]
-- SQL sent before the exception
(no SQL)
== Fix 2: split into two queries (same Lists)
employees=3 projects=4
-- two queries, one collection each
sql> select bd1_0.id,e1_0.dept_id,e1_0.id,e1_0.name from BagDept bd1_0 left join BagEmp e1_0 on bd1_0.id=e1_0.dept_id
sql> select bd1_0.id,p1_0.dept_id,p1_0.id,p1_0.title from BagDept bd1_0 left join BagProject p1_0 on bd1_0.id=p1_0.dept_id where bd1_0.id in (?)
== Fix 1: Set instead of List
employees=3 projects=4
-- one query, two join fetches
sql> select sd1_0.id,e1_0.dept_id,e1_0.id,e1_0.name,p1_0.dept_id,p1_0.id,p1_0.title from SetDept sd1_0 left join SetEmp e1_0 on sd1_0.id=e1_0.dept_id left join SetProject p1_0 on sd1_0.id=p1_0.dept_id
rows the database returned for that one query: 12 (3 x 4: the Cartesian product Hibernate then folds back into 3 + 4 objects)
== Fix 3: keep Lists, drop the join fetch, add @BatchSize
employees=3 projects=4
-- query the parents, touch both collections
sql> select bd1_0.id from BatchDept bd1_0
sql> select e1_0.dept_id,e1_0.id,e1_0.name from BatchEmp e1_0 where e1_0.dept_id=?
sql> select p1_0.dept_id,p1_0.id,p1_0.title from BatchProject p1_0 where p1_0.dept_id=?
+13
View File
@@ -0,0 +1,13 @@
# Set + id-based hashCode vs UUID-based hashCode: remove() after persist
# Hibernate ORM 7.4.16.Final, H2, JDK 25
id-based hashCode: Set.remove(child) returned false
-- commit
sql> insert into IdHashOwner (id) values (default)
sql> insert into IdHashChild (name,owner_id,id) values (?,?,default)
rows left in IdHashChild: 1 (the child was NOT removed, so orphanRemoval never saw it)
UUID-based hashCode: Set.remove(child) returned true
-- commit
sql> insert into departments (name,id) values (?,default)
sql> insert into employees (dept_id,name,uuid,id) values (?,?,?,default)
sql> delete from employees where id=?
rows left in employees: 0