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:
co-authored by
Claude Sonnet 5.5
parent
bdae169002
commit
eab5461d86
@@ -0,0 +1,340 @@
|
||||
package com.ankurm.onetomany;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.OrderColumn;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import org.hibernate.annotations.BatchSize;
|
||||
|
||||
/**
|
||||
* Every other @OneToMany shape the article compares, as small entities with public fields so the mapping is the only thing to read.
|
||||
* Naming: Jt = unidirectional join table, Jc = unidirectional @JoinColumn, Bi = bidirectional, Oc = @OrderColumn.
|
||||
*/
|
||||
public final class Variants {
|
||||
|
||||
private Variants() {
|
||||
}
|
||||
|
||||
// ---- unidirectional, no mappedBy, no @JoinColumn: Hibernate adds a join table ----
|
||||
|
||||
@Entity(name = "JtChild")
|
||||
public static class JtChild {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
public String name;
|
||||
|
||||
protected JtChild() {
|
||||
}
|
||||
|
||||
public JtChild(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "JtListParent")
|
||||
public static class JtListParent {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
public List<JtChild> children = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Entity(name = "JtSetParent")
|
||||
public static class JtSetParent {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
public Set<JtChild> children = new HashSet<>();
|
||||
}
|
||||
|
||||
// ---- unidirectional with @JoinColumn: no join table, the child table carries the foreign key ----
|
||||
|
||||
@Entity(name = "JcChild")
|
||||
public static class JcChild {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
public String name;
|
||||
|
||||
protected JcChild() {
|
||||
}
|
||||
|
||||
public JcChild(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "JcListParent")
|
||||
public static class JcListParent {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
@JoinColumn(name = "parent_id")
|
||||
public List<JcChild> children = new ArrayList<>();
|
||||
}
|
||||
|
||||
// ---- unidirectional @JoinColumn plus @OrderColumn: the list keeps a position column ----
|
||||
|
||||
@Entity(name = "OcChild")
|
||||
public static class OcChild {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
public String name;
|
||||
|
||||
protected OcChild() {
|
||||
}
|
||||
|
||||
public OcChild(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "OcParent")
|
||||
public static class OcParent {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
@JoinColumn(name = "parent_id")
|
||||
@OrderColumn(name = "pos")
|
||||
public List<OcChild> children = new ArrayList<>();
|
||||
}
|
||||
|
||||
// ---- bidirectional with a List (a "bag"): the owning side is the child ----
|
||||
|
||||
@Entity(name = "BiListEmp")
|
||||
public static class BiListEmp {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
public String name;
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "dept_id")
|
||||
public BiListDept department;
|
||||
|
||||
protected BiListEmp() {
|
||||
}
|
||||
|
||||
public BiListEmp(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "BiListDept")
|
||||
public static class BiListDept {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
@OneToMany(mappedBy = "department", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
public List<BiListEmp> employees = new ArrayList<>();
|
||||
|
||||
public void add(BiListEmp e) {
|
||||
employees.add(e);
|
||||
e.department = this;
|
||||
}
|
||||
}
|
||||
|
||||
// ---- two collections on one parent, three ways: bags, sets, bags with @BatchSize ----
|
||||
|
||||
@Entity(name = "BagEmp")
|
||||
public static class BagEmp {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
public String name;
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "dept_id")
|
||||
public BagDept department;
|
||||
|
||||
protected BagEmp() {
|
||||
}
|
||||
|
||||
public BagEmp(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "BagProject")
|
||||
public static class BagProject {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
public String title;
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "dept_id")
|
||||
public BagDept department;
|
||||
|
||||
protected BagProject() {
|
||||
}
|
||||
|
||||
public BagProject(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "BagDept")
|
||||
public static class BagDept {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
@OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
|
||||
public List<BagEmp> employees = new ArrayList<>();
|
||||
@OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
|
||||
public List<BagProject> projects = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Entity(name = "SetEmp")
|
||||
public static class SetEmp {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
public String name;
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "dept_id")
|
||||
public SetDept department;
|
||||
|
||||
protected SetEmp() {
|
||||
}
|
||||
|
||||
public SetEmp(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "SetProject")
|
||||
public static class SetProject {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
public String title;
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "dept_id")
|
||||
public SetDept department;
|
||||
|
||||
protected SetProject() {
|
||||
}
|
||||
|
||||
public SetProject(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "SetDept")
|
||||
public static class SetDept {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
@OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
|
||||
public Set<SetEmp> employees = new HashSet<>();
|
||||
@OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
|
||||
public Set<SetProject> projects = new HashSet<>();
|
||||
}
|
||||
|
||||
@Entity(name = "BatchEmp")
|
||||
public static class BatchEmp {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
public String name;
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "dept_id")
|
||||
public BatchDept department;
|
||||
|
||||
protected BatchEmp() {
|
||||
}
|
||||
|
||||
public BatchEmp(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "BatchProject")
|
||||
public static class BatchProject {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
public String title;
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "dept_id")
|
||||
public BatchDept department;
|
||||
|
||||
protected BatchProject() {
|
||||
}
|
||||
|
||||
public BatchProject(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "BatchDept")
|
||||
public static class BatchDept {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
@OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
|
||||
@BatchSize(size = 20)
|
||||
public List<BatchEmp> employees = new ArrayList<>();
|
||||
@OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
|
||||
@BatchSize(size = 20)
|
||||
public List<BatchProject> projects = new ArrayList<>();
|
||||
}
|
||||
|
||||
// ---- an id-based hashCode on a child inside a Set ----
|
||||
|
||||
@Entity(name = "IdHashChild")
|
||||
public static class IdHashChild {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
public String name;
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "owner_id")
|
||||
public IdHashOwner owner;
|
||||
|
||||
protected IdHashChild() {
|
||||
}
|
||||
|
||||
public IdHashChild(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof IdHashChild c && java.util.Objects.equals(id, c.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return java.util.Objects.hashCode(id); // BUG on purpose: id is null until persist
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "IdHashOwner")
|
||||
public static class IdHashOwner {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
@OneToMany(mappedBy = "owner", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
public Set<IdHashChild> children = new HashSet<>();
|
||||
|
||||
public void add(IdHashChild c) {
|
||||
children.add(c);
|
||||
c.owner = this;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user