package com.ankurm.hibernatedemo.naturalid; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import org.hibernate.annotations.NaturalId; /** * A COMPOSITE natural id: (company, deptCode) together must be unique, not either field alone. * Matches the article's example -- verified to actually load via * {@code session.byNaturalId(Department.class).using(...).using(...).load()}. * *

Docs: docs/06-natural-ids.md, Topic 3. */ @Entity public class Department { @Id @GeneratedValue private Long id; @NaturalId @ManyToOne @JoinColumn(name = "company_id") private Company company; @NaturalId private String deptCode; private String name; public Department() { } public Department(Company company, String deptCode, String name) { this.company = company; this.deptCode = deptCode; this.name = name; } public Long getId() { return id; } public Company getCompany() { return company; } public String getDeptCode() { return deptCode; } public String getName() { return name; } }