Hibernate / Hibernate 7 Basics Interview Questions
How does Hibernate 7 handle primary key generation strategies?
Primary key generation is controlled by @GeneratedValue. Hibernate 7 supports all four JPA strategies plus UUID as a first-class feature.
| Strategy | How it works | Best for |
|---|---|---|
| IDENTITY | DB auto-increment (SERIAL/AUTO_INCREMENT) | Simple setups; disables JDBC batch inserts |
| SEQUENCE | Uses DB sequence; Hibernate batches allocation | PostgreSQL, Oracle; supports batch inserts |
| TABLE | Simulates sequence with a special table | Portable but slow |
| AUTO | Hibernate picks best strategy | Quick prototyping |
| UUID | Generates UUID as PK | Distributed systems |
// IDENTITY: @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; // SEQUENCE (recommended for batch inserts): @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_seq") @SequenceGenerator(name="product_seq", sequenceName="product_id_seq", allocationSize=50) private Long id; // UUID (first-class in Hibernate 7): @Id @GeneratedValue(strategy = GenerationType.UUID) private UUID id; // Time-based UUID v7: @Id @UuidGenerator(style = UuidGenerator.Style.TIME) private UUID id;
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...
