Hibernate Quiz ( Advanced ) - All Questions
This advanced Hibernate quiz is built for experienced Java developers preparing for senior-level interviews and real-world ORM challenges. It covers Hibernate internals, session lifecycle, flushing strategies, caching mechanisms, concurrency control, performance optimization, inheritance strategies, and production-grade Hibernate usage.
Question 1: Why is Session not thread-safe in Hibernate?
- It manages multiple connections
- It maintains first-level cache and persistence context
- It uses lazy loading
- It executes HQL queries
Explanation: Session maintains state and cache, making it unsafe for concurrent access.
Question 2: What is the main role of the persistence context?
- Manage transactions
- Track entity state changes
- Execute queries
- Handle connections
Explanation: Persistence context tracks entity state and changes.
Question 3: What happens during dirty checking?
- Entities are deleted
- Hibernate compares entity state snapshots
- Cache is cleared
- Transactions are committed
Explanation: Hibernate detects changed fields by comparing snapshots.
Question 4: Which flush mode delays SQL execution until commit?
Explanation: COMMIT flushes only at transaction commit.
Question 5: Which flush mode gives complete control to developers?
Explanation: MANUAL requires explicit flush calls.
Question 6: What is the risk of excessive flushing?
- Memory leak
- Performance degradation
- Deadlock
- Data inconsistency
Explanation: Frequent flushes cause unnecessary SQL execution.
Question 7: Which Hibernate feature prevents lost updates?
- Pessimistic locking
- Optimistic locking
- Second-level cache
- Batch processing
Explanation: Optimistic locking detects concurrent updates.
Question 8: Which annotation enables optimistic locking?
- @Lock
- @Id
- @Version
- @Optimistic
Explanation: @Version enables version-based locking.
Question 9: What happens when version mismatch occurs?
- Data overwritten
- Silent update
- OptimisticLockException thrown
- Transaction retries
Explanation: Hibernate throws OptimisticLockException.
Question 10: Which locking strategy blocks concurrent access?
- Optimistic
- Pessimistic
- Versioned
- Deferred
Explanation: Pessimistic locking blocks other transactions.
Question 11: Which cache stores entities across sessions?
- First-level cache
- Session cache
- Second-level cache
- Query cache
Explanation: Second-level cache is shared across sessions.
Question 12: Why should query cache be used cautiously?
- It increases memory usage
- It can return stale data
- It disables indexing
- It blocks writes
Explanation: Query cache may return outdated results.
Question 13: Which cache provider is commonly used with Hibernate?
- Redis
- Ehcache
- Hazelcast
- All of the above
Explanation: Hibernate supports multiple cache providers.
Question 14: What problem does second-level cache NOT solve?
- Repeated reads
- Database load
- Stale data risk
- Read scalability
Explanation: Cache consistency issues still exist.
Question 15: Which fetch strategy solves the N+1 select problem?
- Lazy loading
- Fetch join
- Immediate fetching
- Auto flush
Explanation: Fetch join loads associations in one query.
Question 16: Why is EAGER fetching risky in production?
- Compilation errors
- Excessive SQL joins and performance issues
- Data loss
- Deadlocks
Explanation: EAGER fetching may load unnecessary data.
Question 17: Which inheritance strategy uses a discriminator column?
- JOINED
- TABLE_PER_CLASS
- SINGLE_TABLE
- MAPPED_SUPERCLASS
Explanation: SINGLE_TABLE uses discriminator column.
Question 18: Which inheritance strategy normalizes database design most?
- SINGLE_TABLE
- JOINED
- TABLE_PER_CLASS
- SUBCLASS
Explanation: JOINED normalizes data using multiple tables.
Question 19: What is the main drawback of TABLE_PER_CLASS?
- Complex joins
- Duplicate columns
- Discriminator usage
- Limited polymorphism
Explanation: TABLE_PER_CLASS duplicates columns.
Question 20: Which feature improves bulk insert performance?
- Caching
- Batch processing
- Lazy loading
- Query cache
Explanation: Batch processing reduces round trips.
Question 21: Which setting enables JDBC batching?
- hibernate.fetch_size
- hibernate.jdbc.batch_size
- hibernate.cache.use
- hibernate.flush.mode
Explanation: hibernate.jdbc.batch_size enables batching.
Question 22: Why should Session be short-lived?
- Reduce memory footprint
- Avoid stale data and cache growth
- Prevent deadlocks
- Improve syntax
Explanation: Long-lived sessions increase memory and stale data.
Question 23: What is the effect of clear() on Session?
- Deletes database data
- Clears first-level cache
- Commits transaction
- Closes session
Explanation: clear() removes all entities from cache.
Question 24: What is the difference between evict() and clear()?
- No difference
- evict removes one entity, clear removes all
- clear deletes data
- evict flushes data
Explanation: evict removes one entity, clear removes all.
Question 25: Which Hibernate API is type-safe?
- HQL
- Native SQL
- Criteria API
- JPQL
Explanation: Criteria API provides type safety.
Question 26: Why is Criteria API preferred in large applications?
- Less code
- Compile-time safety and refactoring support
- Better performance
- Caching
Explanation: Criteria API supports refactoring safely.
Question 27: Which problem occurs when detached objects are updated incorrectly?
- Memory leak
- NonUniqueObjectException
- Deadlock
- LazyInitializationException
Explanation: Hibernate throws NonUniqueObjectException.
Question 28: What causes LazyInitializationException?
- Closed Session with lazy association access
- Missing annotations
- Cache failure
- Deadlock
Explanation: Lazy data accessed after session close.
Question 29: Which pattern avoids LazyInitializationException?
- DAO
- Open Session in View
- Singleton
- Factory
Explanation: Open Session in View keeps session open.
Question 30: Why is Open Session in View controversial?
- Complex configuration
- Leads to long transactions
- Breaks ORM
- Disables caching
Explanation: It may cause long-running transactions.
Question 31: Which Hibernate feature handles DB-specific SQL?
- Session
- Dialect
- Transaction
- Criteria
Explanation: Dialect abstracts database differences.
Question 32: Why should native queries be minimized?
- Security risk
- Reduced portability
- Lower performance
- Compilation errors
Explanation: Native SQL reduces database portability.
Question 33: Which concurrency strategy is best for read-heavy systems?
- Pessimistic locking
- Optimistic locking
- No locking
- Serializable isolation
Explanation: Optimistic locking works best with low contention.
Question 34: What is the impact of excessive second-level caching?
- Better performance always
- Memory pressure and stale data
- Deadlocks
- Slower GC
Explanation: Caching too much increases memory usage.
Question 35: Which Hibernate feature ensures transactional consistency?
- Caching
- Dirty checking
- ACID transactions
- Lazy loading
Explanation: Transactions ensure ACID properties.
Question 36: Why should equals() and hashCode() be implemented carefully?
- Performance
- Entity identity consistency
- Serialization
- Caching only
Explanation: Incorrect implementations break identity.
Question 37: Which entity identifier strategy is safest for distributed systems?
- IDENTITY
- SEQUENCE
- TABLE
- UUID
Explanation: UUID avoids DB coordination.
Question 38: What is the main drawback of IDENTITY strategy?
- No portability
- Breaks batching
- Duplicate keys
- Large memory usage
Explanation: IDENTITY disables JDBC batching.
Question 39: Which Hibernate feature improves read scalability most?
- Batch updates
- Second-level cache
- Flush mode
- Pessimistic locking
Explanation: Second-level cache improves read scalability.
Question 40: Which issue arises with long-running transactions?
- Better consistency
- Lock contention and stale data
- Lower memory usage
- Faster execution
Explanation: Long transactions hold locks longer.
Question 41: Which approach best supports Hibernate scalability?
- Single Session per app
- Session per request
- Global transactions
- Eager fetching
Explanation: Session per request scales well.
Question 42: Why is Hibernate considered an ORM and not just a query tool?
- It generates SQL
- It manages object lifecycle and state
- It uses HQL
- It supports caching
Explanation: Hibernate manages entity lifecycle and state.
Question 43: Which practice best improves Hibernate maintainability?
- Hardcoded queries
- Clear entity boundaries
- EAGER fetching
- Global Session usage
Explanation: Clear entity design improves maintainability.
Question 44: What is the ultimate goal of mastering Hibernate?
- Avoid SQL completely
- Build scalable and maintainable persistence layers
- Improve UI rendering
- Reduce JVM usage
Explanation: Hibernate mastery enables scalable persistence design.