Hibernate Framework Hibernate Performance Tuning 1 — Questions and Answers
Question 1: Which Hibernate feature allows bulk loading of lazy collections more efficiently?
- Batch fetching with @BatchSize (Correct answer)
- Eager loading with FetchType.EAGER
- Named queries
- Second-level cache
Correct answer: Batch fetching with @BatchSize
@BatchSize configures Hibernate to load multiple lazy collections in a single batch query instead of individual queries per entity.
Question 2: What is a StatelessSession in Hibernate?
- A session without a first-level cache for bulk data operations (Correct answer)
- A session that cannot persist data
- A read-only session
- A session for schema operations
Correct answer: A session without a first-level cache for bulk data operations
StatelessSession bypasses the first-level cache and is optimized for bulk insert/update/delete operations where caching overhead is undesirable.
Question 3: Which Hibernate feature improves bulk INSERT performance?
- JDBC batch inserts via hibernate.jdbc.batch_size (Correct answer)
- Second-level cache
- Lazy loading
- Connection pooling
Correct answer: JDBC batch inserts via hibernate.jdbc.batch_size
Setting hibernate.jdbc.batch_size batches multiple INSERT statements into a single JDBC call, dramatically reducing round trips for bulk inserts.
Question 4: What does FetchType.EAGER do to query performance?
- Can cause excessive data loading and degrade performance with large datasets (Correct answer)
- Always improves performance
- Reduces the number of queries
- Has no performance impact
Correct answer: Can cause excessive data loading and degrade performance with large datasets
EAGER loading always fetches associations, even when not needed, which can result in loading huge datasets and degrading performance.
Question 5: What is the purpose of database connection pooling in Hibernate?
- Reuses existing database connections to avoid the overhead of creating new ones (Correct answer)
- Stores query results in memory
- Manages multiple databases simultaneously
- Batches SQL statements
Correct answer: Reuses existing database connections to avoid the overhead of creating new ones
Connection pooling maintains a pool of reusable database connections, eliminating the cost of creating and tearing down connections for each request.
Question 6: Which strategy avoids the N+1 problem without changing the entity fetch type globally?
- Using JOIN FETCH in HQL for specific queries (Correct answer)
- Setting all associations to EAGER
- Enabling the second-level cache
- Using StatelessSession
Correct answer: Using JOIN FETCH in HQL for specific queries
JOIN FETCH in HQL loads associations in the same query as the parent entity, solving N+1 only for that specific query without affecting the global fetch strategy.
Which Hibernate feature allows bulk loading of lazy collections more efficiently?