Hibernate / Hibernate 7 Basics Interview Questions
What is the @NaturalId feature in Hibernate 7 and what is @NaturalIdClass?
A natural ID is a business-meaningful unique identifier distinct from the surrogate PK. Hibernate 7 adds @NaturalIdClass for composite natural IDs.
@Entity public class Book { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; // surrogate PK (internal) @NaturalId // business identifier (external) @Column(unique=true, nullable=false) private String isbn; private String title; } // Optimised lookup (uses L2 cache): Book book = session.byNaturalId(Book.class) .using("isbn", "978-0-13-468599-1").load(); // @NaturalIdClass: COMPOSITE natural IDs (NEW in Hibernate 7) public class EmployeeNaturalId { String companyCode; String employeeNumber; // equals() and hashCode() required } @Entity @NaturalIdClass(EmployeeNaturalId.class) // NEW in Hibernate 7 public class Employee { @Id @GeneratedValue private Long id; @NaturalId String companyCode; @NaturalId String employeeNumber; private String name; } // Lookup by composite natural ID: Employee emp = session.byNaturalId(Employee.class) .using("companyCode", "ACME") .using("employeeNumber", "EMP-001").load();
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
