Prev Next

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.

@GeneratedValue strategies
StrategyHow it worksBest for
IDENTITYDB auto-increment (SERIAL/AUTO_INCREMENT)Simple setups; disables JDBC batch inserts
SEQUENCEUses DB sequence; Hibernate batches allocationPostgreSQL, Oracle; supports batch inserts
TABLESimulates sequence with a special tablePortable but slow
AUTOHibernate picks best strategyQuick prototyping
UUIDGenerates UUID as PKDistributed 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;

Why does GenerationType.IDENTITY disable JDBC batch inserts in Hibernate 7?
Which GenerationType is recommended for PostgreSQL to maximise bulk insert performance?

Invest now in Acorns!!! 🚀 Join Acorns and get your $5 bonus!
Acorns Logo

Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!

Earn passively and while sleeping

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.

Robinhood Logo

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 Logo

Webull! Receive free stock by signing up using the link: Webull signup.

More Related questions...

What is Hibernate 7 and when was it released? What is ORM and why is Hibernate used instead of writing raw JDBC? How do you configure Hibernate 7 with Maven? What is an entity in Hibernate 7 and how do you define one? What are entity states in Hibernate 7 and how do they transition? What is the biggest breaking change in Hibernate 7: detached entity reassociation removal? What is the Session in Hibernate 7 and what are its core CRUD operations? What is the Hibernate SessionFactory and how do you create one? What is HQL (Hibernate Query Language) in Hibernate 7? What is JPQL vs HQL in Hibernate 7? How does Hibernate 7 handle primary key generation strategies? What is the first-level cache (persistence context) in Hibernate 7? What is the second-level cache in Hibernate 7 and how does it work with StatelessSession? What are lazy and eager fetching in Hibernate 7 and how do you avoid the N+1 problem? How do you map one-to-many and many-to-one relationships in Hibernate 7? What is the StatelessSession in Hibernate 7 and when should you use it? What is the Criteria API in Hibernate 7 and what are SelectionSpecification and RestrictionSpecification? How does @Embeddable and @Embedded mapping work in Hibernate 7? What are the new FindOptions in Jakarta Persistence 3.2 and Hibernate 7? What is optimistic locking in Hibernate 7 and how do you implement it? What is pessimistic locking in Hibernate 7 and what are the improvements? What is the @ManyToMany relationship in Hibernate 7 and how do you map it? What is Hibernate's flush behaviour and how does it affect SQL execution? What are named queries in Hibernate 7 and how are they type-safe with TypedQueryReference? What is the @Inheritance mapping in Hibernate 7 and what strategies are available? What is the @Filter and auto-enabled filters feature in Hibernate 7? What is key-based pagination in Hibernate 7 and how does it differ from offset pagination? What is Jakarta Data 1.0 and how does it integrate with Hibernate 7? What is the @NaturalId feature in Hibernate 7 and what is @NaturalIdClass? How does Hibernate 7 support vector data types for AI/ML applications? What are the key removed APIs in Hibernate 7 and how do you migrate? What is @SQLRestriction and @SQLJoinTableRestriction in Hibernate 7? How does Hibernate 7 handle schema validation and generation (hbm2ddl)? What are Hibernate 7's improvements to multi-tenancy support? What is the @Check constraint and @ColumnDefault annotation in Hibernate 7? What is @BatchSize and how does it solve the N+1 problem differently from JOIN FETCH? What is Hibernate 7's @Struct mapping for database composite types? What are Hibernate 7's logging improvements for SQL and parameter binding? How does Hibernate 7 work with Spring Boot 4 - what is auto-configured? What are the key Hibernate 7 vs Hibernate 6 differences interviewers ask about?
Show more question and Answers...

Maven

Comments & Discussions