0% found this document useful (0 votes)
13 views

JPA_Hibernate_Interview_Problems

The document discusses JPA and Hibernate, focusing on entity lifecycle management, fetch types, and caching mechanisms. It outlines the key states of JPA entities, differences between EAGER and LAZY fetching, solutions for the N+1 select problem, and implications of CascadeType in entity relationships. Additionally, it explains the first-level and second-level caching in Hibernate, highlighting their benefits and configurations.

Uploaded by

pbecic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

JPA_Hibernate_Interview_Problems

The document discusses JPA and Hibernate, focusing on entity lifecycle management, fetch types, and caching mechanisms. It outlines the key states of JPA entities, differences between EAGER and LAZY fetching, solutions for the N+1 select problem, and implications of CascadeType in entity relationships. Additionally, it explains the first-level and second-level caching in Hibernate, highlighting their benefits and configurations.

Uploaded by

pbecic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

JPA and Hibernate for Strong Senior Developers

How does JPA manage entity lifecycle and what are the key states?
 JPA entities can be in one of the following states:
 New (Transient): not associated with persistence context.
 Managed (Persistent): associated with EntityManager and tracked for changes.
 Detached: was persistent, but EntityManager is closed or cleared.
 Removed: marked for deletion but not yet deleted from DB.
 EntityManager operations:
 persist(): makes new entity persistent.
 merge(): copies state into a managed entity.
 detach(): removes from persistence context.
 remove(): marks entity for deletion.

What are the differences between fetch types EAGER and LAZY in JPA?
 EAGER:
 Related entities are fetched immediately using JOINs.
 May cause N+1 problems or performance overhead.
 LAZY:
 Related entities are loaded only when accessed.
 Requires open persistence context; otherwise, LazyInitializationException.
 Best practice:
 Use LAZY by default and explicitly JOIN FETCH when necessary.

How do you handle the N+1 select problem in JPA/Hibernate?


 The N+1 problem occurs when a parent entity fetches multiple child entities
individually.
 Solutions:
 Use JOIN FETCH in JPQL or Criteria API.
 Use EntityGraph to load required associations.
 Batch fetching in Hibernate (hibernate.default_batch_fetch_size).
 Always monitor queries using logging or tools like Hibernate Statistics.

What are the implications of using CascadeType in entity relationships?


 CascadeType defines operations propagated from parent to child entities.
 Common types:
 PERSIST — propagates persist().
 MERGE — propagates merge().
 REMOVE — propagates remove().
 ALL — applies all cascade operations.
 Use carefully to avoid accidental deletions or unintended operations.
 Avoid CascadeType.REMOVE on @ManyToOne relationships to prevent deleting shared
references.

How does Hibernate's first-level and second-level cache work?


 First-level cache:
 Built into EntityManager (Session).
 Each persistence context maintains a local cache of managed entities.
 Second-level cache:
 Optional shared cache (e.g., Ehcache, Infinispan).
 Configured per entity or collection — requires setup in persistence.xml or Spring config.
 Query cache (optional):
 Caches query result sets — needs explicit enablement and careful invalidation.
 Use cases:
 Reduces database hits, especially for frequently accessed read-only data.

You might also like