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,60 @@
|
||||
package com.ankurm.onetomany;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/** The recommended shape: bidirectional, child owns the foreign key, a Set, helper methods on the parent. */
|
||||
@Entity
|
||||
@Table(name = "departments")
|
||||
public class Department {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
// mappedBy = "department" names the field on Employee that owns the foreign key column.
|
||||
@OneToMany(mappedBy = "department", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
|
||||
private Set<Employee> employees = new HashSet<>();
|
||||
|
||||
protected Department() {
|
||||
}
|
||||
|
||||
public Department(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
// The invariant that keeps both sides of the association in step.
|
||||
public void addEmployee(Employee employee) {
|
||||
employees.add(employee);
|
||||
employee.setDepartment(this);
|
||||
}
|
||||
|
||||
public void removeEmployee(Employee employee) {
|
||||
employees.remove(employee);
|
||||
employee.setDepartment(null);
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Set<Employee> getEmployees() {
|
||||
return employees;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.ankurm.onetomany;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** The one-to-many side with no fetch attribute: JPA's default for @OneToMany is LAZY. */
|
||||
@Entity
|
||||
public class EagerDepartment {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
|
||||
private List<EagerEmployee> employees = new ArrayList<>();
|
||||
|
||||
protected EagerDepartment() {
|
||||
}
|
||||
|
||||
public EagerDepartment(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public List<EagerEmployee> getEmployees() {
|
||||
return employees;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.ankurm.onetomany;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
|
||||
/** A child that leaves @ManyToOne at its default, to show what the default loads. */
|
||||
@Entity
|
||||
public class EagerEmployee {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
// No fetch attribute: JPA's default for @ManyToOne is EAGER.
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "dept_id")
|
||||
private EagerDepartment department;
|
||||
|
||||
protected EagerEmployee() {
|
||||
}
|
||||
|
||||
public EagerEmployee(String name, EagerDepartment department) {
|
||||
this.name = name;
|
||||
this.department = department;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.ankurm.onetomany;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
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.Table;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
/** The owning side. equals/hashCode use a UUID assigned at construction, so they stay stable from new() to persist() to detach. */
|
||||
@Entity
|
||||
@Table(name = "employees")
|
||||
public class Employee {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
@Column(nullable = false, updatable = false)
|
||||
private UUID uuid = UUID.randomUUID();
|
||||
|
||||
// LAZY: the JPA default for @ManyToOne is EAGER; this line is what opts out.
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "dept_id")
|
||||
private Department department;
|
||||
|
||||
protected Employee() {
|
||||
}
|
||||
|
||||
public Employee(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Department getDepartment() {
|
||||
return department;
|
||||
}
|
||||
|
||||
public void setDepartment(Department department) {
|
||||
this.department = department;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof Employee e && Objects.equals(uuid, e.uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(uuid);
|
||||
}
|
||||
}
|
||||
@@ -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