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
+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 (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)